-1

hello i have a question about some SQL query that keep give me an error
Code :

$result = mysql_query("SELECT email FROM users WHERE user_id='$uid' and set emailchange=1");  

the query keep giving me an error
any help ?

  • 3
    `and set emailchange=1` ?!? WTF! Do you actually have a column name called `set emailexchange` with a space in it? – Mark Baker Sep 06 '14 at 08:37
  • @MarkBaker no emailchange is other column that i want to change to 1 – user3813111 Sep 06 '14 at 08:39
  • @Dagon no error just the query don't work at all – user3813111 Sep 06 '14 at 08:40
  • you cant create a select like that you could do an update with a sub-querry select however. No error, because you you don't properly check for one –  Sep 06 '14 at 08:40
  • You're trying to UPDATE and to SELECT in a single statement? If you need to do the select as well, then do this as two separate statements... – Mark Baker Sep 06 '14 at 08:41
  • so there is no way to make this query work ? @Dagon – user3813111 Sep 06 '14 at 08:41
  • yes , can i do this ? @MarkBaker – user3813111 Sep 06 '14 at 08:42
  • 1
    `$result1 = mysql_query("SELECT email FROM users WHERE user_id='$uid'")` and `$result2 = mysql_query("UPDATE email set emailchange=1 WHERE user_id='$uid'");` – Mark Baker Sep 06 '14 at 08:43
  • 5
    Clearly you're a beginner..... drop whatever tutorial you're using, stop using the MySQL extension, and find a tutorial that uses MySQLi or PDO and that teaches about prepared statements and bind variables – Mark Baker Sep 06 '14 at 08:44
  • can i use the ";" or something to separate query's? @MarkBaker – user3813111 Sep 06 '14 at 08:46
  • @MarkBaker can it be `$result = mysql_query("SELECT email FROM users WHERE user_id='$uid';UPDATE email set emailchange=1 WHERE user_id='$uid'"); `? – user3813111 Sep 06 '14 at 08:52
  • @user3813111 - No!!! Do what I've said, learn to use MySQLi or PDO, and use bind variables, and use two statements. In addition to being insecure, the MySQL extension is officially deprecated in PHP – Mark Baker Sep 06 '14 at 08:55

3 Answers3

0

Try following query

$result = mysql_query("SELECT email FROM users WHERE user_id=$uid and emailchange=1");
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
0

You can also update after select the data:

$result = mysql_query("SELECT email FROM users WHERE user_id='$uid' and set emailchange=1");  
if($result){
$result2 = mysql_query("UPDATE email set emailchange=1 WHERE user_id='$uid'");  
}
Priya jain
  • 753
  • 2
  • 5
  • 15
0

The error is caused by the set here:

and set emailchange=1

You can't select and update in the same SQL statement, you need to execute the update statement then write a select statement to grab the email - assuming that is what you mean to do so:

Update the field:

$result = mysql_query("UPDATE users set emailchange = 1 where user_id='$uid'");

select the data:

$result2 = mysql_query("select email from users where user_id='$uid'");

You really should escape $uid before passing it to the query and ideally you should be using PDO!