chiprivers Posted September 29, 2010 Share Posted September 29, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/214771-mysqli-non-object-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 29, 2010 Share Posted September 29, 2010 Your prepare statement failed. A) You must ALWAYS check if a statement worked or not before you blindly use the result of that statement, B) If you echo $mysqli->error; it will tell you why the prepare failed. Quote Link to comment https://forums.phpfreaks.com/topic/214771-mysqli-non-object-error/#findComment-1117367 Share on other sites More sharing options...
chiprivers Posted September 30, 2010 Author Share Posted September 30, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/214771-mysqli-non-object-error/#findComment-1117546 Share on other sites More sharing options...
PFMaBiSmAd Posted September 30, 2010 Share Posted September 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214771-mysqli-non-object-error/#findComment-1117554 Share on other sites More sharing options...
chiprivers Posted September 30, 2010 Author Share Posted September 30, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/214771-mysqli-non-object-error/#findComment-1117556 Share on other sites More sharing options...
PFMaBiSmAd Posted September 30, 2010 Share Posted September 30, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/214771-mysqli-non-object-error/#findComment-1117557 Share on other sites More sharing options...
chiprivers Posted September 30, 2010 Author Share Posted September 30, 2010 Not sure what I have done but it is not working!? Just retyped it in and it seems to be fine. I did check them all but it is possible that I missed a spelling mistake on a column name! Quote Link to comment https://forums.phpfreaks.com/topic/214771-mysqli-non-object-error/#findComment-1117561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.