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:
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!
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.
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: PHP: mysqli::query - Manual
The documentation also has examples in which they check retrieve the query error, which should help you figure this out too.