Can't make a prepared statement work

Hello!

Here’s my code snippet. The $query object is always false. I threw some debugging statements in there.


function db_connect()
{
   $handle = new mysqli('localhost', 'username', 'password', 'database_name'); 
   if (!$handle)
   {
     return false;
   }
   return $handle;
}

$handle = db_connect();

$query = $handle->prepare("insert into publications (PublicationName, FileName, FileSize, Category, CreatedBy, DatePublished, DateCreated) values (?, ?, ?, ?, ?, ?, NOW())");		
if($query)
{
	$query->bind_param("ssisss", $PublicationName, $NewFileName, $FileSize, $Category, $_SESSION['auth_user'], $PublishDate);
	$query->execute();
}
else
{
	if (mysqli_connect_errno())
	{
		printf("Connect failed: %s\
", mysqli_connect_error());
		exit();
	}
	else
	{
		echo 'So, $query is not an object, but there is not a connection error?';
		exit();
	}
}

The echo statement “So, $query is not an object…” is all I ever get. I don’t understand why $query isn’t an object. Any ideas?

Do you have a valid $handle?

For reference, here is the (trimmed) example code from the mysqli_prepare documentation page.


$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\
", mysqli_connect_error());
    exit();
}
$city = "Amersfoort";
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
    $stmt->bind_param("s", $city);
    $stmt->execute();
    $stmt->bind_result($district);
    $stmt->fetch();
    printf("%s is in district %s\
", $city, $district);
    $stmt->close();
}
$mysqli->close();

I’m not sure. I figured that if the handle was invalid, the prepare method would fail. However, the prepare method seems to fire just fine. That function that returns the handle is in an include file that is used all over the site without issue.

Any suggestions?

What information do you have when you var_dump $query?

You could also try having a look at $query->error()

var_dump returns: bool(false)

trying to echo $query->error() gets me:
Fatal error: Call to a member function error() on a non-object

Which is similar to the errors I get when I try to call bind_param and execute.

So either $handle->prepare has problems, or $handle has problems. Does that make sense?

It makes sense, I just don’t know what the issue is or what to check for. Even the example at PHP.net doesn’t check for failure of the prepare statement. If mysqli returns a object with no error, prepare should just work.

I tried switching to:

$query = mysqli_prepare($handle,“sql statement…”);

But, I got the same error. WTH??

echo $handle->error;

http://www.php.net/manual/en/mysqli.error.php

As the name of the function suggests, mysqli_connect_error() provides an error message about connecting to the db failing.

Right there with you crmalibu! I was just doing that when you posted. I have a problem with a column name. Yay! Now, maybe I get this done and go get some dinner. :stuck_out_tongue: