Query works fine in phpmyadmin returns 0 lines when launched with php

I'm really shocked by this. Here is the php:

Update: I escaped input (before I did it differently, but didn't know about mysql_real_escape_string). In addition, I replace double quotes with single quotes, but the same problem occurs. Below is the updated code:

$request = mysql_real_escape_string($_POST['id']); $colName = mysql_real_escape_string($_POST['col_name']); function executeAssoc1($q) { $r = mysql_query($q) or die(mysql_error() . ' ' . $q); $rass = mysql_fetch_assoc($r); return $rass; } foreach(array_keys($_POST) as $pitem) { if($pitem == (...what I want it to...)) { $pitem_name = mysql_real_escape_string(rawurldecode($pitem)); $qf = "SELECT * FROM possible_values WHERE table_id=$request AND col_name='$colName' AND value = '$pitem_name'"; $qfr = executeAssoc1($qf); var_dump($pitem_name); echo '<br>'; var_dump($qf); echo '<br>'; var_dump($qfr); } } 

Here is part of what this code outputs in a single loop:

line (37) "1. New England (Northeast Region)"
string (122) "SELECT * FROM possible_values ​​WHERE table_id = 3 AND col_name = 'DIVISION' AND value = '1. New England (Northeast Region)'"
bool (false)

Now, when I copy this query to the phpmyadmin SQL editor, it really returns the result. I even tried to use LIKE "% ...%" as suggested in here , but the same thing happens (phpmyadmin returns a string, php returns 0 rows).

+4
source share
4 answers

From the rawurldecode () manual:

Note: rawurldecode () does not decrypt plus characters ('+') into spaces. urldecode () does.

if you output your mySQL query, I'm sure your string will look something like this:

 1+.New+England+(Northeast+region) 

try urldecode () as manual write suggests.

+2
source

you should use single quotes instead of double quotes, i.e.:

 SELECT * FROM possible_values WHERE table_id=3 AND col_name='DIVISION' AND value = '1 .New England (Northeast region)' 
0
source

Double quotes are not allowed for strings in SQL, as they are used for identifiers (e.g. MySQL uses the back of ``). I think MySQL allows them to use strings with some settings, but you should not use them.

Technically, this should return an error, so I don't think your problem is.

What could be your problem: you are using the old mysql extension, which may not support your version of mysql.

0
source

It may be a typo, but

 table_id=$request 

should have quotes around him:

 table_id='$request' 
0
source

Source: https://habr.com/ru/post/1300809/


All Articles