Can't get database search to work

Username epiz_23464544

Error shown:
This page isn’t working [URL] is currently unable to handle this request HTTP ERROR 500

Other Information
The PHP code to access my database table is as follows and works perfectly well but when I try to add a user’s search field, all I get is the message above:

<?php 
  try {
  include 'connect.php';
  $query = "SELECT * FROM Bath_Wells_NBR";
  //first pass just gets the column names
  print "<table>";
  $result = $con->query($query);
  //return only the first row (we only need field names)
  $row = $result->fetch(PDO::FETCH_ASSOC);
  print " <tr>";
  foreach ($row as $field => $value){
   print " <th>$field</th>";
  } // end foreach
  print " </tr>";
  //second query gets the data
  $data = $con->query($query);
  $data->setFetchMode(PDO::FETCH_ASSOC);
  foreach($data as $row){
   print " <tr>";
   foreach ($row as $name=>$value){
   print " <td>$value</td>";
   } // end field loop
   print " </tr>";
  } // end record loop
  print "</table>";
  } catch(PDOException $e) {
   echo 'ERROR: ' . $e->getMessage();
  } // end try
 ?>

The ‘form’ code is as follows and appears to work okay:

<form action="bathwells_testpage.php" method="POST">
	<input type="text" name="founder" placeholder="Enter Founder's Surname">
	<button type="submit" name="submit-founder">Search</button>
</form>

I have tried copying the code from a YouTube tutorial and applying it to suit my own database but with the same ‘page not working’ error message. The copied code is as follows:

 <?php 
include 'connect.php';
if (isset($_POST['submit-founder'])) {
$search = mysqli_real_escape_string ($con, $_POST['founder']);
$sql = "SELECT DATE, Founder, FROM Bath_Wells_NBR  WHERE Founder LIKE '%founder%' ORDER BY DATE";
$result = mysqli_query($con, $sql); 
$queryResult = mysqli_num_rows($result);
echo "There are ".$queryResult." results";
if($queryResult > 0) {
while (row = mysqli_fetch_assoc($result)) {
echo "<div>

	 <p> ".$row['DATE']."</p>
	 <p> ".$row['Founder']."</p>
	 
	 </div>";
	 }
	 } else {
	 echo "There are no results";
	 }
	 }
?>

Is this because I have a free hosting account and would need to upgrade for this page to work? I am at a loss!

Hello there,

Please refer to this KB article if you haven’t already:

A HTTP ERROR 500 simply tells you that the PHP code has crashed, but gives no information as to why it has crashed.

4 Likes

The ‘copied’ code worked fine in the tutorial demo but doesn’t on my site and I cannot see where I have gone wrong (if at all).

Then please read that KB article I provided you above, it should help you to find out your real error message. If you were able to find out why your code crashed and doesn’t work then it should help you to narrow down your issue and you might be able to fix it on your own.

4 Likes

Thank you. This is now showing up distinct errors. I am now struggling to translate them into plain English but still have not cured the problem.

Try enabling PHP Errors in the Control Panel. Then you can see what is actually causing the error.

3 Likes

Just paste this Code into your .htaccess file.

Code:-

1 Like

If you want, you can share the errors here so we can have a look and maybe help figure out what to do next.

2 Likes

Thank you Admin. I have removed the actual text from my connection code which is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php 

$mysqli = new mysqli("host","user_name","password","database_name");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

if (isset($_POST['submit-founder'])) {
$search = mysqli_real_escape_string ($mysqli, $_POST['founder']);
$sql = "SELECT DATE, Founder, FROM Bath_Wells_NBR  WHERE Founder LIKE '%founder%' ORDER BY DATE";
$result = mysqli_query($mysqli, $sql[int $resultmode = MYSQLI_STORE_RESULT ]:mixed); 
if(mysqli_num_rows($result)>0); {
while ($row = mysqli_fetch_assoc($result)){
echo "<div>
	<p>".$row['DATE']."</p>
	<p>".$row['Founder']."</p>
</div>";
	 }
	 }
	 } else {
	 echo "There are no results";
	 }
?>


</body>
</html>

This is showing the error:
Parse error: syntax error, unexpected ‘$resultmode’ (T_VARIABLE), expecting ‘]’ in … on line 22

Sorry, this is line 22: $result = mysqli_query($mysqli, $sql[int $resultmode = MYSQLI_STORE_RESULT ]:mixed);

Try removing the “int” that you have before the variable. I believe that is why it’s breaking.

Now getting:
mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given on line 23
and:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given on line 24

A boolean result from mysqli_query means it’s probably returning FALSE, which means the query failed. Here is the documenation: https://www.php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues

The documentation also has examples in which they check retrieve the query error, which should help you figure this out too.

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.