Jump to content

Recommended Posts

$exper = $mysqli->prepare("UPDATE `userpets` SET `experience` = experience + ? WHERE `petid` = ? AND `owner` = ?") or die($mysqli->error . mysqli_error($mysqli));
	$exper->bind_param('iss', $gain_this_much, $grab_pet_id, $userid);
	$exper->execute();
	$exper->store_result();
	//var_dump($mysqli);
	$exper->fetch();
	$exper->close();

The code below gives me the error : "Fatal error: Call to a member function prepare() on a non-object . . . . " However there is no non-object. I use the EXACT SAME code on another page and no error. Updates correctly on other page also just not on this one page where it gives this error.

Anyways to fix this? ALL ideas are welcome. I have tried query also. No luck. Does anyone know why this happens? I had this happen before , but I don't remember what I did to fix it. I know it was simple. Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/308493-says/
Share on other sites

2 minutes ago, Barand said:

Have you connected to the database server? And is that connection $mysqli?

Yes I have. On another page but I have it in a php include.

Not sure if this helps, but it is something I noticed and tested. If I use include() it gives no errors, but if I use require_one() or include_once() that's when it shows the error. (for the file with the connection)

Edited by cutielou22
Link to comment
https://forums.phpfreaks.com/topic/308493-says/#findComment-1565468
Share on other sites

you have a variable scope or naming problem or a connection problem. the entire error message, which you didn't post enough of, states if the bad value is a null or a boolean.

it would take having all the code needed to reproduce the problem, less the database connection credentials, in order to help you.

Link to comment
https://forums.phpfreaks.com/topic/308493-says/#findComment-1565500
Share on other sites

Here is the whole error: Fatal error: Call to a member function prepare() on a non-object in /home3/solpetsc/public_html/funcs.inc.php on line 522

It's this whole function:

I did no edits so you can see my tests. I would post the page I am using the function on where it is not working. But it is not working on any of them. Also the error is showing up now without the below - or die code.

Quote

or die($mysqli->error . mysqli_error($mysqli));

I did used to get a error about the " AND `owner` = ?" part. Not sure how I was getting that error - cannot reproduce it now to get EXACTLY what it said, but it was talking about a nonobject and the " AND `owner` = ?" part.

Switching the petid and owner order still caused the "AND . . . " problem.

function gain_experience($gain_this_much, $grab_pet_id){

	include_once "addon.inc.php";
	//include "addon.inc.php";

	//if(!$mysqli->query("UPDATE `userpets` SET `experience` = experience + $gain_this_much WHERE `owner` = '$userid' AND `petid` = '$grab_pet_id'") or die(mysqli_error($mysqli))){
	$exper = $mysqli->prepare("UPDATE `userpets` SET `experience` = `experience` + ? WHERE `petid` = ? AND `owner` = ?");
	//or die($mysqli->error . mysqli_error($mysqli));
	$exper->bind_param('iss', $gain_this_much, $grab_pet_id, $userid);
	$exper->execute();
	$exper->store_result();
	//var_dump($mysqli);
	$exper->fetch();
	$exper->close();
	
	//die("Error description: " . mysqli_error($mysqli));
	//}
	
	//////////////////// CHECK IF NEED LEVEL UPGRADE ////////////////////
	
	$stmt = $mysqli->prepare("SELECT level, experience FROM `userpets` WHERE owner = ? AND petid = ? LIMIT 1");
	$stmt->bind_param('ss', $userid, $grab_pet_id);
	$stmt->execute();
	$stmt->store_result();
	$stmt->bind_result($current_level, $new_experience);
	$stmt->fetch();
	$stmt->close();
	
	$current_levelm = $current_level - 1;
	$current_levelp = $current_level + 1;
	
	$check_experience = $current_level * 100;
	
	if (($new_experience >= $check_experience) && ($current_level > $current_levelm) && ($current_level < $current_levelp)) {
	
		$stmt3 = $mysqli->prepare("UPDATE `userpets` SET level=level+1 WHERE owner = ? AND petid = ?");
		$stmt3->bind_param('ss', $userid, $grab_pet_id);
		$stmt3->execute();
		$stmt3->store_result();
		$stmt3->fetch();
		$stmt3->close();
		
	}
	
	//return 1;

}

 

Edited by cutielou22
Link to comment
https://forums.phpfreaks.com/topic/308493-says/#findComment-1565501
Share on other sites

the function code does not have access to the connection (the error message should state more, but php has been making less than desirable changes to error messages lately.) you also likely don't have php's error_reporting set to E_ALL, as there should be a second error about $mysqli not being defined.

if the connection is being made in addon.inc.php, it won't exist if addon.ini.php has been previously included. your main code should make the database connection, then pass it into any function that needs it as a call-time parameter. you would then remove any attempt at creating a connection inside the functions.

 

2 minutes ago, cutielou22 said:

Taking out the part after the "AND " still creates the same "Fatal error: Call to a member function prepare() on a non-object in /home3/solpetsc/public_html/funcs.inc.php on line 522" error.

that's because, as has been stated in each reply in this thread, the problem is with the $mysqli variable, it's what is not an object.

 

 

Link to comment
https://forums.phpfreaks.com/topic/308493-says/#findComment-1565503
Share on other sites

Okay. Gotcha. I added the below.

error_reporting(E_ALL); // Report all errors

Now 2 errors come up. So you guys are right. But what would cause the $mysqli error? Just a duplicate like said above? Even though I inserted my database connection directly in the function and renamed it and that didn't work either. Glad I am kind of getting somewhere though.

Notice: Undefined variable: mysqli in /home3/solpetsc/public_html/funcs.inc.php on line 525

Fatal error: Call to a member function prepare() on a non-object in /home3/solpetsc/public_html/funcs.inc.php on line 525

Link to comment
https://forums.phpfreaks.com/topic/308493-says/#findComment-1565505
Share on other sites

25 minutes ago, cutielou22 said:

Even though I inserted my database connection directly in the function and renamed it and that didn't work either.

you would have to post that version of the code if you want help with it.

here are some points about the code that will simplify it and keep you from having to keep changing the error handling -

1) UPDATE queries do not return result sets. the ->store_result(), ->fetch(), and ->close() statements after each update query are unnecessary.

2) if you use exceptions for errors and let php catch and handle the exception, it will use its' error_reporting, display_errors, and log_errors settings to control what happens with the actual error information. you would then remove all the error handling logic in your code. to enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

3) the petid should be unique and there isn't any apparent $userid variable in the function. the queries should only need the petid value. any user to pet ownership should have been determined in the php code prior to calling the function.

4) the level is derived from the experience and should just be calculated when needed and not stored. this will eliminate 2/3's of the code.

5) the php PDO extension is simpler and more consistent than the mysqli extension. if you can, switch to use PDO.

lastly, you should not just update a value in a row for doing this, since there is no audit trail. you will never know if a logic mistake, double form submission, or nefarious use has caused the value to be incorrectly altered. you should INSERT a new row in the table for each transaction that affects the value, along with all the who, what, when, where, and why information about each transaction. then, to get the current value, you would just SUM() the amounts in the query, GROUPed BY the petid.

Edited by mac_gyver
  • Great Answer 1
Link to comment
https://forums.phpfreaks.com/topic/308493-says/#findComment-1565507
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.