Hey. I need some help with getting a record from a mysql database and compare it with data already stored in a php variable. If they don't match then I want to save the record in the database under another field and then update it. Here is my code:

$query = "SELECT * FROM players WHERE UID='$uid';
$result=mysql_query($query);

$count=mysql_num_rows($result);

if($count==1){

$row = mysql_fetch_array($result) or die(mysql_error());

if($nick!={$row['Nick']})
{
mysql_query("UPDATE players SET NickOld = {$row['Nick']} AND Nick = '$nick' WHERE UID = '$uid'");
}

So what I do is check if $nick is equal to the data stored in the field namned Nick in database. If not I want to save that record to another field namned NickOld and then set the Nick field to the new data. But I get the error "unexpected T_STRING" on this line

mysql_query("UPDATE players SET NickOld = {$row['Nick']} AND Nick = '$nick' WHERE UID = '$uid'");

So, is it merely a syntax error? Or am I approaching it the wrong way?

You didnt close the first query

$query = "SELECT * FROM players WHERE UID='$uid'";//<<<<----- here

You got your quotes wrong.
Change

$query = "SELECT * FROM players WHERE UID='$uid';

to

$query = "SELECT * FROM players WHERE UID='$uid'";

And use a decent editor with syntax highlighting. I recommend EditPad++.

Thanks guys! I noticed it eventually, never done that much PHP and that combined with using notepad was a huge fail. Need to get a proper editor.

Anyway after solving that I stumbled upon another error, it simply refused to update the players accordingly. It wouldn't throw an mysql error, it simply didn't update even though there was a matching record in db to update. So I used mysql_info() and saw I had warnings on the update query. Took me a while to figure out cause I didn't know how to retrieve the warnings (the query thing in phpmyadmin didn't complain either). Eventually I removed "AND" from the update query and simply used a comma which made the query execute as expected and update the record!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.