简体   繁体   中英

Empty mysql_query, can't figure it out

I'm having trouble with making this query to work, I have 2 tables one with client info and the other one with product info. Trying to join them while querying the db.

mysql_query(
    "SELECT client.id, client.email, client.prodid, prod.id, prod.name
    FROM client, prod
    WHERE client.id ="'.mysql_real_escape_string($_GET["id"]).'"
    AND client.prodid =prod.id"
);

But that query doesn't return anything. What am I doing wrong? Thanks in advance.

Your quotes are wrong.

You use " to start the string in your method. In the middle you use "'.mysql_real_escape_string($_GET["id"]).'" . Notice that you use "' instead of '" .

This should work better (from a PHP point of view, I did not check your SQL syntax):

mysql_query(
    "SELECT client.id, client.email, client.prodid, prod.id, prod.name
    FROM client, prod
    WHERE client.id ='".mysql_real_escape_string($_GET["id"])."'
    AND client.prodid =prod.id"
);

Your quotes appear to be cancelling out the dynamic ID variable. If your client.id field is numeric, then you should remove the single quotes surrounding mysql_real_escape_string():

mysql_query(
 "SELECT client.id, client.email, client.prodid, prod.id, prod.name
 FROM client, prod
 WHERE client.id = ".mysql_real_escape_string($_GET["id"])." AND client.prodid = prod.id"
);

If it works in PHPMyAdmin then the query's fine. Enable debugging and use var_dump():

ini_set('display_errors','on');
error_reporting(E_ALL);
$result = mysql_query($sql_query);
var_dump($result);

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