0

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.

Devon Bernard
  • 2,250
  • 5
  • 19
  • 32

2 Answers2

2

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->error also helps.

There are 2 way of reporting errors:

  • displaying them on-screen (decent at development but unacceptable on production)
  • writing them in log (an absolute must on a production server)

there are some ini settings responsible for this behavior.

  • display_errors tells PHP wheither it should display errors or not
  • log_errors tells PHP wheither it should log errors or not. by default it logs into web-servers' error_log
  • error_log tells php the filename where PHP errors have to be written

please 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

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I am on a new host so I am not 100% sure how their logs work. But when I checked the only type of error I could see was that "Class 'mysqli' not found in..." on the line where I set my $dbconn value. Does this mean I have to install 'MySQLi' in my program? – Devon Bernard Jan 17 '13 at 18:19
1

The first thing that I see is that the line:

$dbconn = new mysqli("localhost", "my_user", "my_password", "my_table");

Should be:

$dbconn = new mysqli("localhost", "my_user", "my_password", "my_db");

Try that and see what happens.

bensiu
  • 24,660
  • 56
  • 77
  • 117
Robert
  • 11
  • 1