last day (15 days later) » 

9:23 PM
1
Q: PHP pdo only gets result when query is hardcoded

oderflaVery strange behaviour. Using PHP 7. When I run this query: $sql = "SELECT * FROM product WHERE model = 'prefix_17867'"; $db_res = $db->query($sql); $db_row = $db_res->fetch(PDO::FETCH_ASSOC); print_r($db_row); An array is printed with the fetched row. Array ( [product_id] => 18108 [model] =>...

 
where's $code defined? Also, you're open to SQL injection and should read up on using prepared statements
 
sorry. $code is a variable that only contains the string '17867'
 
Both those code fragments are open to SQL Injection Attack. Even if you are escaping inputs, its not safe! You should consider using prepared parameterized statements in either the MYSQLI_ or PDO API's instead of concatenated values
You do know that $variables are automatically expanded in PHP when used inside a double quoted string? So that like could be simply written as $sql = "SELECT * FROM product WHERE model = '$prfx' ";`
 
I faced the same issue few years back.. The issue was a Unicode_Character which was not visible... But I am not sure about this case, Can you please Hash both hardcoded and dynamic $prefix and check both hashes are same?
 
Can you try using $prfx = "prefix_17867"? If it works, then the problem is possibly $code
 
9:23 PM
Try echo $sql; and see what it shows
 
as I said, echoing will give exactly the same string printed on the screen.
 
What about doing $db->quote($prfx) ?
 
no luck. im also using prepared statements now.sigh.
 
this is the usual case of a linefeed character :)
 
how to solve this thing with linefeed character?
 
9:23 PM
print_r($db_row); has an empty Array ?
 
well trim() but you need to learn how to solve such problems in general
 
I did try with trim but with no luck.
no. its not an empty array. it doesn't write anything at all.
 
var_dump it to know what type it is !
 
it gives a bool(false)
 
var_dump($sql) from both versions and compare.
 
9:23 PM
Okay... there is necessarily error on your query ! execute just print_r($db->errorInfo()); after calling of query method !
 
The error is an array. It looks like this: Array ( [0] => 00000 [1] => [2] => )
 
Oupps! no error... Let's dig it in other facing
 
mm yeah...doing var_dump on first hardcoded and then concatenated string, gives an array with the fields first time and a bool(false) second time. and no, no errors sigh.
 
Are you sure prefix_17867 and $prfx is equal ? In other words, are you sure that prefix_17867 == $prfx is true ?
 
Follow this instruction to find out what's wrong with your $code variable.
 
9:23 PM
the truth is that when I concatenate the variable, its a field fetched from a csv file. I debugged the value and I cannot see any strange things. When I echo it I cannot see anything at the beginning or at the end of the string.
 

last day (15 days later) »