简体   繁体   中英

PHP different results for mysql_query and pdo

I am trying to convert a mysql_query to pdo equivalent

My table structure is (removed unrelated columns - the one needed for my question is other_id ) :

CREATE TABLE `temp_table` (
  `id` int(12) NOT NULL DEFAULT '0',
  `other_id` int(12) NOT NULL DEFAULT '0',
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

And the data it has:

-----------------
| id | other_id |
-----------------
| 1  |      123 |
-----------------
| 2  |        0 |
-----------------
| 3  |      456 |
-----------------
| 4  |        0 |
-----------------

The previous database query was :

$sql = "SELECT id FROM temp_table WHERE other_id = '{$other_id}'";
$result = mysql_query($sql, $db)
return mysql_fetch_assoc($result);

The query is called with $other_id as NULL ("null" in php and not string or anything).

Result mysql_query : This gives my values 2,4

PDO equivalent code :

$sql = "SELECT id FROM temp_table WHERE other_id = :other_id";
$sth = $dbConnection->prepare($sql);
$sth->bindValue(":other_id", $other_id);
$sth->execute();
return $sth->fetchAll(PDO::FETCH_COLUMN);

Result PDO : This gives no values at all.

This is weird issue which I have not encountered before (since I am more java developer and recently touched PHP after few years).

As a workaround I had to put below line to return 2,4 as result from pdo output, but want to understand more about the difference mentioned above.

$sth->bindValue(":other_id", empty($other_id) ? 0 : $other_id);

I also tried $sth->bindValue(":other_id", $other_id, PDO::PARAM_INT); which did not help

This is not an issue with the mysql_ vs PDO difference, but rather with type-juggling.

In the first non-PDO example, $other_id is being converted to a string in the query, so your query looks like this:

SELECT id FROM temp_table WHERE other_id = '';

MySQL treats an empty string the same as 0 for fields of numeric type during queries, so your query is actually correctly matching two records.

In the PDO example since you're passing $other_id (which is null ), your query is bound as follows:

SELECT id FROM temp_table WHERE other_id = NULL;

MySQL does not treat 0 as NULL , so the queries that you're sending are actually not the same.

This is weird issue which I have not encountered before.

There is nothing weird here.

When $other_id is NULL the first query becomes:

SELECT id FROM temp_table WHERE other_id = ''

Because the type of the other_id column is numeric, the provided value (the empty string) is converted to the number 0 and there are two matching rows.

On the other hand, the query sent through PDO is equivalent to:

SELECT id FROM temp_table WHERE other_id = NULL

Not only that there are no NULL s in the table, but this query never returns any row, even if there are rows having NULL in the column other_id . (The correct way to select the rows having NULL in other_id is to use the IS NULL operator ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM