Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysqli fails to prepare statement 1

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
0
0
CA
Hi,

I'm getting the following error "Fatal error: Call to a member function bind_param() on a non-object" when I call $statement->bind_param('i', $featuredSpecialID); in the following code:
Code:
	function removeSpecial() {
		global $HOST_NAME, $USER_NAME, $PASSWORD, $DATABASE;

		$featuredSpecialID = $_POST['featuredSpecialID'];
		$mysqli = new mysqli($HOST_NAME, $USER_NAME, $PASSWORD, $DATABASE);
		$sql = 'DELETE FROM featured_special WHERE featured_special_id = ?';
		$statement = $mysqli->prepare($sql); '*** error seems to be here
		$statement->bind_param('i', $featuredSpecialID); '*** crashes here
		$statement->execute();
		$mysqli->close();
	}
It seems to be unable to prepare the statement. The funny thing is, $featuredSpecialID = 5 and when I run "DELETE FROM featured_special WHERE featured_special_id = 5" in phpMyAdmin, the statement deletes the corresponding row without any problem.

I'm running XAMPP 1.7.2 (PHP v5.3.0) on Windows 7 Home Premium.

I've already confirmed that the user name, host name, database variables are all correct. Any help is appreciated!

TIA
 
Oops when I pasted the code above, I accidentally used VB style comments with ' instead of C-style //. My actual code doesn't have any comments there, so that's not the problem. I just included comments here for clarity.
 
try this instead. it should yield better information. the error you are getting indicates that the sql query has not been properly prepared. normally this happens when the query is incorrect: the table name or column names could be incorrect

Code:
function removeSpecial() {
	global $HOST_NAME, $USER_NAME, $PASSWORD, $DATABASE;
	
	$featuredSpecialID = $_POST['featuredSpecialID'];
	$mysqli = new mysqli($HOST_NAME, $USER_NAME, $PASSWORD, $DATABASE);
	//check connection
	if (mysqli_connect_errno()) {
    	printf("Connect failed: %s\n", mysqli_connect_error());
    	exit();
	}
	
	$sql = 'DELETE FROM featured_special WHERE featured_special_id = ?';
	if ($statement = $mysqli->prepare($sql)){
		$statement->bind_param('i', $featuredSpecialID); 
		$result = $statement->execute();
		$mysqli->close();
		return $result;
	} else {
		printf("Error message: %s\n", $mysqli->error);
		$mysqli->close();
		return false;
	}
}
 
Ahhhh yes, that was helpful! This time I got the following output "DELETE command denied to user ..." which is correct. I completely forgot to check if the user account had delete permission (which of course it doesn't)!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top