visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Databases :: MySql :: How should I use 'WHERE $var1 = $var2' in mysql ?


By: Squibi U.S.A.  Date: 22/03/2003 00:00:00  English  Points: 100 Status: Answered
Quality : Excellent
Hello people,
I am using PHP, and I have a problem using a simple mysql code which does not like it like this:

result = "SELECT * FROM my_table WHERE '$id' = '$curr_id'";

It runs but doesn't give me proper result.

It works for:
result = "SELECT * FROM my_table WHERE ID = '$curr_id'";
but it doesn't like above statement.

Could you please let me know what is the corrct syntax.


Sincerely,

By: VGR Date: 22/03/2003 04:59:00 English  Type : Answer
it's correct. Try to remove the single quotes, they are not needed in SQL92 nor in MySql

So do :

$query = "SELECT * FROM my_table WHERE $charfieldname = '$somestringvalue';";

In fact, this is used if $charfieldname contains a valid field/column name for the table, and if it's a column of type char() or varchar() - or derived types DATE, TIME, DATETIME, TIMESTAMP etc ; else (it's numerical) remove the single quotes around the tested value $somevalue.

if your column name is 'id', as I suspect, - and I assume that 'id' is an integer column - then the correct syntax is :
$query = "SELECT * FROM my_table WHERE id = $curr_id;";


By: VGR Date: 22/03/2003 05:00:00 English  Type : Comment
moreover, if ever you try your first query with, say, $id=4 and $curr_id=2, you in fact ask for :
result = "SELECT * FROM my_table WHERE '4' = '2'";

which is ridiculous, as you will understand.
By: sumotimor Date: 25/03/2003 07:42:00 English  Type : Comment
What is the $id ?

If it is a variable in your page, then you have to write:

result = "SELECT * FROM my_table WHERE '$curr_id' = '$id'";

If you are reading both $curr_id and $id from my_table, you cannot get results because basicly there are no rows which $curr_id and $id has same value...

Regards
By: VGR Date: 25/03/2003 07:46:00 English  Type : Comment
listening PHPnewbee... I gave you the answer...
is it working now ?

Do register to be able to answer

EContact
browser fav
page generated in 321.589950 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page