Jump to content

mysqli non-object error


chiprivers

Recommended Posts

I am getting the following error on my mysqli prepare statement / bind parameters script:

 

Fatal error: Call to a member function bind_param() on a non-object in...

 

The script is:

 

$mysqli = new mysqli(constant('DB_HOST'), constant('DB_USERNAME'), constant('DB_PASSWORD'), constant('DB_DATABASE'));
$insertStatus = $mysqli->prepare("
	INSERT INTO status
	(contractId, start, end, statusCodeId, resourceId, recordCreated, recordCreatedBy)
	VALUES
	(?, ?, ?, ?, ?, ?, ?)
");
$insertStatus->bind_param('issiisi', $contractId, $start, $end, $statusCodeId, $resourceId, $recordCreated, $recordCreatedBy);

 

I can't work out what the error is? I have tested the database connection and that is fine, as is the query statement!

Link to comment
Share on other sites

Modified code:

$mysqli = new mysqli(constant('DB_HOST'), constant('DB_USERNAME'), constant('DB_PASSWORD'), constant('DB_DATABASE'));
   $insertStatus = $mysqli->prepare("
      INSERT INTO status
      (contractId, start, end, statusCodeId, resourceId, recordCreated, recordCreatedBy)
      VALUES
      (?, ?, ?, ?, ?, ?, ?)
   ");

  echo $mysqli->error;

   if ($insertStatus) {
     $insertStatus->bind_param('issiisi', $contractId, $start, $end, $statusCodeId, $resourceId, $recordCreated, $recordCreatedBy);
   }

 

However there is no error being returned!

Link to comment
Share on other sites

I'm going to guess that your connection code is failing ( new mysqli() returns an object even if the connection fails so that you can reference $mysqli->connect_errno and $mysqli->connect_error.) This would mean that $mysqli->prepare() itself throws a Warning: mysqli::prepare() Couldn't fetch mysqli in ... error in this case.

 

Are you doing this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors will be reported and displayed?

 

You also need to test if your connection works before blindly attempting to use it.

 

Also, why are using using the constant() function to get the constant values, you just use the constant name directly.

Link to comment
Share on other sites

I'm going to guess that your connection code is failing ( new mysqli() returns an object even if the connection fails so that you can reference $mysqli->connect_errno and $mysqli->connect_error.) This would mean that $mysqli->prepare() itself throws a Warning: mysqli::prepare() Couldn't fetch mysqli in ... error in this case.

 

I have tried echoing $mysqli->connect_error but nothing is returned.

 

Are you doing this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors will be reported and displayed?

 

Error reporting is set to all, except notices.

 

Also, why are using using the constant() function to get the constant values, you just use the constant name directly.

 

I had not used constants before and the tutorial I found when googling was using the constant() function to reference the constant values.  I have changed this now and use the constant name itself.

 

All above considered, it is still not working!

Link to comment
Share on other sites

I've tested using your original code, short of actually making the columns in the table, and the information I have been posting is based on the errors I have gotten.

 

Based on your latest code and symptom, it is likely that the $insertStatus->bind_param() is failing (it returns a bool true/false, in which case $insertStatus->error will tell you why it failed if it returned a false value) or everything you have shown is working, but you are no longer executing the query in your code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.