Reputation: 5
I have a working php code which simply connects database lists the data and closes it. I embedded this working php code into my html but it does not parse the rest of the html. It is simple blank after listing data from database and php close tag " ?> ". I tried to get rid of " $db->disconnect(); " in php and now it works.
Do you have any idea why? Do i really need to use $db->disconnect(); every time?
Thanks
beginning of the html code
<ul>
<?php
$db = new mysqli('localhost', 'root', 'root', 'troy');
if (mysqli_connect_errno()) {
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
$query = "SELECT * FROM platters";
$result = $db->query($query);
while($row = mysqli_fetch_array($result)) {
echo "<li>" . $row['name'] . "</li>";
}
$db->disconnect();
?>
</ul>
rest of the html code
Upvotes: 0
Views: 2625
Reputation: 74058
Run it on the command line
php test.php
and see the error message
PHP Fatal error: Call to undefined method mysqli::disconnect() in test.php on line 28
This is the reason, why it outputs everything from the database, but omits everything after that line.
When you remove the $db->disconnect();
statement, the error is gone and the PHP script can work it's way until the end.
Upvotes: 1
Reputation: 5524
$db->disconnect();
is not listed under the documentation, is this a custom function that you have created?
Instead of disconnecting the entire database connection, you should just perform:
$result->close();
this will free up the current query, and enable you to perform further connections throughout that PHP Script.
Only close the connection to the database entirely if you are done with your SQL Connection for that script
Edit
After re-reading your question, $db->disconnect();
is not a function for MySQli. See the entire documentation here:
http://php.net/manual/en/book.mysqli.php
You should use:
$db->close();
which closes the current connection to the database (if used on your database variable)
http://www.php.net/manual/en/mysqli.close.php
Upvotes: 1