Converting to MySQLi


I’m new to MySQLi but I have a lot of experience in MySQL

I have a routine to backup a row from one table to a second table using the following commands:

	$sql_history = "INSERT INTO history ( SELECT * FROM table WHERE username = '" . $username . "')";
	if(!$result = mysql_query($sql_history))
    		die(mysql_error().'<BR> SQL_HISTORY: ' . $sql_history);

This works well.

I need help to convert these commands into MySQLi

Here is what I have so far:

$sql_history = ‘INSERT INTO history ( SELECT * FROM table WHERE username = ?)’;
$stmt1 = $db->prepare($sql_history);
$stmt1->bind_param(‘s’, $username);


However I receive the following error:

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

Any help will be appreciated

What does your script think $stmt1 is? var_dump($stmt1);
If it shows that it thinks $stmt1 is a boolean false (may also show up as int(0)), your prepare attempt failed, and you should echo out $db->error to see what error message was returned.

Also i’m… moderately sure you dont need the parenthesis around your SELECT. You may be confusing the database engine into thinking you’re trying to define field names.


Thank you for your reply.

It helped me narrow down the error. It was an oversight on my part. I forgot the prefix the database name in the command.

I got it working by adding database name to the table name.

Like this:

$sql_history = ‘INSERT INTO database.history ( SELECT * FROM database.table WHERE username = ?)’;

Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.