Search code examples
phpmysqlierrnoprepare

Prepared statement getting error 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;
   }
?>

Solution

  • 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.