Jump to content

Says


cutielou22

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
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)

Link to comment
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
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;

}

 

Link to comment
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
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
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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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