shotdsherrif shotdsherrif - 8 months ago 104
PHP Question

mysqli::query() expects parameter 2 to be long, string given

I am in the middle of creating a custom shopping cart and I', building a query that begins with retrieving a session_id I just saved into the carts table. I know this value was saved, and I run this query at the mysql command line and it returns just what I need BUT I am not getting the value into the $cart_id. There are other INSERT queries above and below this point in the script so I know I'm connecting to the db just fine.

//Get cart id
$cart_id_select_q = "SELECT FROM carts AS c WHERE c.user_session_id='$uid'";
$cart_id = $mysqli->query($conn, $cart_id_select_q);
echo "<pre>Debug: $cart_id_select_q</pre>";
if ( !$cart_id ) {
printf("error: %s\n", mysqli_error($conn));
else {
echo 'get session id from cart: execute success';

I'm also on a VPS server with errors suppressed not I'm not getting a mysqli_error to display in the browser but I am getting the following 2 warnings in my error_log.

[29-Jul-2011 09:29:24] PHP Warning: mysqli::query() expects parameter 2 to be long, string given in /home/sopadmin/public_html/dev/cart.php on line 89
[29-Jul-2011 09:29:24] PHP Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /home/sopadmin/public_html/dev/cart.php on line 92

I've also tried to use mysqli_num_rows() and fetch_assoc() but none have helped. the $cart_id remains null and I don't know how to retrieve the mysql error in this server configuration. Posting here is a last resort after toying with it all night.

I should note, I am just starting to really use the new mysqli extension and I'm also beginning to code in a more OO way. But I'm usually starting out coding procedurally and then creating classes when I have the design laid out. But that's just a comment on my experience level, what I don't understand is why this query is not returning a vlue inside my script.

Answer Source

When using object oriented mysqli API, you do not need (in fact you can't) pass a connection as the first argument.

This: $cart_id = $mysqli->query($conn, $cart_id_select_q);

should be: $cart_id = $mysqli->query($cart_id_select_q);

And this: printf("error: %s\n", mysqli_error($conn));

should be: printf("error: %s\n", mysqli->error);

I've no idea what $conn is (looks like a string), but it surely is not a MySQLi connection object.