Jump to content

Query works from command line with hardcoded values, fails as parameterised query


Go to solution Solved by Jacques1,

Recommended Posts

Hi all, 

 

I received the following error :

 

 

PHP Fatal error: Uncaught Error:
Call to a member function bind_param() on boolean in /var/www/path/to/f_function.php:411\nStack trace:\n
#0 /var/www/path/to/ver_mail.php(35): activate(Object(mysqli), 157, '6bcc5bcc1fe5d25...')\n
#1 {main}\n  thrown in /var/www/path/to/f_function.php on line 411
 
when I run this query :
 
        $query = "SELECT count(ID), ts FROM table WHERE ID = ? && mcode = ? && active = ?";
        $stmt = $fcon->prepare($query);
        $stmt->bind_param('isi',$fid,$mcode,$zero);

it bombs. I have checked the values of $fid, mcode and $zero and they are correct. 

 

So when I run the same query with hard coded values in mysql command line, 

Database changed
mysql> SELECT count(ID), ts FROM table WHERE ID = 157 && mcode = '6cbb5bcc1fe5d25d7cedb4567b0e2d1c2f36874ad0fd0cf841863999d5160260' && active = 0;
+-----------+------------+
| count(ID) | ts         |
+-----------+------------+
|         1 | 1495508954 |
+-----------+------------+
1 row in set (0.00 sec)

it works fine !!!??

 

I'll be grateful for any ideas or pointers to try and debug this.

 

Thanks all !

 

 

  • Solution

Your mysqli error reporting is still broken. Please scan your entire code for all connections that don't have exceptions enabled, and fix this once and for all. When a query fails, you want a proper error message, not this boolean stuff.

 

Then post the error message here.

 

The query again makes no sense to me, but that's another story.

  • Like 1

Hi Guru Jacques,

 

Thanks loads ! I have fixed that permanently by including the code for that (mysqli error reporting)  in the connection. It was there but commented. I must have been trying something. 

 

The query required to be GROUPed. It has worked.

 

The query is to activate an account. I need to check the "mailcode" and the "active" fields  for the user whose ID is the third parameter. So I first check if the account is not already active and then activate it. The account can be deactivated by the admins.

 

So please tell me what's the other story ?

 

Thanks loads !

When you select a record by its ID, there's by definition exactly one record. Why would you want to count one record? You might also get an empty result set if the ID is wrong, but then again, why would you want to count that? This is a simple existence check.

 

Actually, COUNT(ID) doesn't just count the records. It first excludes all records where ID is NULL (which is impossible with a primary key), and then it counts the remaining rows. This doesn't make any sense whatsoever.

 

Also, UPDATE queries already have their own WHERE clause (which is much more reliable, because it works atomically). Just do your update and then check the number of affected rows -- if that's even important.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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