Databases :: MySql :: How should I use 'WHERE $var1 = $var2' in mysql ? |
|||
| By: Squibi |
Date: 22/03/2003 00:00:00 |
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 | 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 | 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 | 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 | Type : Comment |
|
| listening PHPnewbee... I gave you the answer... is it working now ? |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








