I am trying to make a simple SELECT query to my database with a MySQLi prepared statement. I have read numerous tutorials and followed them very closely but for some reason it is still not working. Below is the query code:
<?php
$user = 'abc';
$dbconn = new mysqli("localhost", "my_user", "my_password", "my_table");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($stmt = $dbconn->prepare("SELECT id, pass FROM my_table WHERE user=?")) {
$stmt->bind_param("s",$user);
$stmt->execute();
$stmt->bind_result($db_id,$db_pass);
$stmt->fetch();
$stmt->close();
}
$dbconn->close();
echo $db_id . ' ' . $db_pass;
?>
When I run this is does not say I have a connection error but a server error. I have a decent amount of PHP experience but am completely new to MySQLi prepared statements. Any help would be greatly appreciated; thank you for your time.
For someone who have a decent amount of PHP experience you are quite light-minded about error reporting.
As you didn’t provide an error message, we can only guess that it is regular web-server’s 503 response.
In this case you have to search server’s error_log for the actual error message, read it, and take appropriate actions to fix it.
Checking
$dbconn->erroralso helps.There are 2 way of reporting errors:
there are some ini settings responsible for this behavior.
display_errorstells PHP wheither it should display errors or notlog_errorstells PHP wheither it should log errors or not. by default it logs into web-servers’ error_logerror_logtells php the filename where PHP errors have to be writtenplease set appropriate settings using your hosting tools.
Otherwise you’d be unable to use this host anyway – a programmer just can’t work if he cant’s see error messages.
Once you get your hands on the error message, it is extremely useful to forward it directly into google search form. Most likely someone already encountered the same and even asked it on Stackoverflow