A
Anonymous
Guest
Below is a test example of the problem I was able to reproduce using prepared statements for a MySQL query. When using a static query it works fine but as soon as I attempt a prepared param the results change. It has something to do with the column schema type I know. See the 2nd code section for more details.
My data in database/table test.test:
clientname,age
"John Doe",55
"Jane Doe",32
"Bob Smith",21
"Rick James",39
The code below only returns John Doe 55, Jane Doe 32, and Rick James 39. It does not return Bob Smith 21 even though that is > 3???
Result = Array(
[0] => Array([pkey] => 1 [clientname] => John Doe [age] => 55)
[1] => Array([pkey] => 2 [clientname] => Jane Doe [age] => 32)
[2] => Array([pkey] => 4 [clientname] => Rick James [age] => 39)
)
Now the column type for age is VARCHAR and when changing it to INT it does resolve the problem and that part makes sense. What does not make sense is if I type the query in in either of the following ways it works with all 4 names returned. What is different about prepared params that cause it to act different with trying to calculate a number using a VARCHAR type column?
Results = Array(
[0] => Array([pkey] => 1 [clientname] => John Doe [age] => 55)
[1] => Array([pkey] => 2 [clientname] => Jane Doe [age] => 32)
[2] => Array([pkey] => 3 [clientname] => Bob Smith [age] => 21)
[3] => Array([pkey] => 4 [clientname] => Rick James [age] => 39)
)
This also resolves the problem.
So I know the solutions do make sense but I like to learn as much as I can and was wondering if anyone knows some details behind the curtain on how prepared params work and would cause it to just not return one row out of four? I would think if it is just a schema problem or cast value type it would not return any rows but it returns 3/4 rows when the problem occurs.
Thanks
My data in database/table test.test:
clientname,age
"John Doe",55
"Jane Doe",32
"Bob Smith",21
"Rick James",39
The code below only returns John Doe 55, Jane Doe 32, and Rick James 39. It does not return Bob Smith 21 even though that is > 3???
Code:
$connector = new PDO('mysql:host=localhost', 'user', 'password');
$comm = $connector -> prepare("SELECT * FROM test.test WHERE age > :age;");
$age = 3;
$comm -> bindParam(':age', $age);
$comm -> execute();
$dataArray = $comm -> fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($dataArray);
echo "</pre>";
$comm = null;
$connector = null;
[0] => Array([pkey] => 1 [clientname] => John Doe [age] => 55)
[1] => Array([pkey] => 2 [clientname] => Jane Doe [age] => 32)
[2] => Array([pkey] => 4 [clientname] => Rick James [age] => 39)
)
Now the column type for age is VARCHAR and when changing it to INT it does resolve the problem and that part makes sense. What does not make sense is if I type the query in in either of the following ways it works with all 4 names returned. What is different about prepared params that cause it to act different with trying to calculate a number using a VARCHAR type column?
Code:
$comm = $connector -> prepare("SELECT * FROM test.test WHERE age > 3;");
$comm = $connector -> prepare("SELECT * FROM test.test WHERE age > '3';");
[0] => Array([pkey] => 1 [clientname] => John Doe [age] => 55)
[1] => Array([pkey] => 2 [clientname] => Jane Doe [age] => 32)
[2] => Array([pkey] => 3 [clientname] => Bob Smith [age] => 21)
[3] => Array([pkey] => 4 [clientname] => Rick James [age] => 39)
)
This also resolves the problem.
Code:
$comm -> bindParam(':age', $age, PDO::PARAM_INT);
So I know the solutions do make sense but I like to learn as much as I can and was wondering if anyone knows some details behind the curtain on how prepared params work and would cause it to just not return one row out of four? I would think if it is just a schema problem or cast value type it would not return any rows but it returns 3/4 rows when the problem occurs.
Thanks