0

Trying to make a prepare statement but for some reason it fails me and i'm getting errno 0 with error (text) being blank. What is causing this? Have been searching the web for a quite while now.

<?php 
   $dbh = new mysqli("localhost","root","","honeypot");

   if ($dbh->connect_errno) {
       echo "Connection failed: (" . $dbh->connect_errno . ") " . $dbh->connect_error;  
       die();
   }

   //Prepare
   if (!($stmt = $dbh->prepare("SELECT tblUsers WHERE UserName = ?"))) {
       echo "Prepare failed: (" . $dbh->connect_errno . ") " . $dbh->connect_error;
   }
?>
  • Because you aren't executing the query, and you aren't binding anything.... Look at your `//Prepare` if. – Darren Oct 12 '14 at 09:55
  • @Darren Why would not executing the query cause `prepare()` to return an error? – Barmar Oct 12 '14 at 10:00

1 Answers1

0

You're getting error 0 because you're printing $dbh->connect_error, but you didn't have an error making the connection. For everything other than the initial connection you should use $dbh->error.

echo "Prepare failed: (" . $dbh->errno . ") " . $dbh->error;

You're getting an error because your query has a syntax error. It should be:

SELECT col1, col2, col3, ... FROM tblUsers WHERE UserName = ?

You're missing the list of columns and the FROM keyword.

Barmar
  • 741,623
  • 53
  • 500
  • 612