1

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.

2 Answers 2

3

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"
);
4
  • +1 I was writing up the same answer but you got here first! :-) I'm having a hard time seeing how the OP's code works at all. Dec 13, 2012 at 0:59
  • If client.id is numeric then the single quotes can go as well.
    – hansvedo
    Dec 13, 2012 at 1:03
  • Tried all those, still empty query. If I query the db directly via phpmyadmin it works, but not embeded within PHP. Very strange. Dec 13, 2012 at 1:16
  • Do you get PHP errors? As you should have had those earlier due to the wrong usage of your quotes. If not please update your question with details about your tables, how you open the connection to your database and all other tings that might be relevant!
    – Veger
    Dec 13, 2012 at 1:24
1

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);
1
  • Didn't solve the problem but is a specific error being displayed?
    – hansvedo
    Dec 17, 2012 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.