ram ram - 1 month ago 8
PHP Question

What does this mean mysqli_query() expects parameter 1 to be mysqli?

Gives me an error stating mysqli_query() expects parameter 1 to be mysqli, dont know what parameter is missing?


$item_id = 1001; $quantity=5;

$mysql_host = 'localhost';
$mysql_user_name='root';
$mysql_user_password='';
$db="ecommerce";

$conn=mysql_connect($mysql_host,$mysql_user_name,$mysql_user_password,$db) or die("could not connect to mysql");;



$sql = "INSERT INTO cart (item_id,qty)VALUES ('$item_id','$quantity')";

$query = mysqli_query($conn, $sql) or die(mysqli_error($conn));


if (query === TRUE)
{
echo "New record created successfully";
}
else
{
echo("Error description: " . mysqli_error($conn));// gives an error here
}

mysql_close();
?>

Answer

You are connecting with mysql_ and trying to query using mysqli_. The two are totally separate and cannot be mixed.

Use something like this:

$mysqli = new mysqli('127.0.0.1', 'your_user', 'your_pass', 'db_name');

$sql = sprintf("INSERT INTO cart (item_id,qty) VALUES ('%d','%d')", $item_id, $quantity);
$result = $mysqli->query($sql);