Page 1 of 1

SELECT statement after UPDATE calls old data

Posted: Fri Nov 26, 2010 4:59 am
by chricholson
I have a PHP page which updates a database record and immediately after selects the data again for display on the page. The problem I am having is the SELECT statement loads the OLD data, regardless of the fact that the select statement appears after the update query. However, looking at the data in the database the information has been updated correctly, so if I then refresh the page sure enough it shows the correct data.

Code: Select all

$songId = $id[0];
	
		$updateQry = mysql_query("UPDATE hits SET song = '".$titles[0]."', artist = '".$artists[0]."', year = ".$years[0].", chart = ".$charts[0].", month = '".$months[0]."', quantity = ".$quantity[0]." WHERE ID = ".$songId) or die(mysql_error());
		
		
		$selectQry = mysql_query("SELECT * FROM hits WHERE id = ".$songId);
		while($row = mysql_fetch_array($result))
		{
				echo "<strong>New Data:</strong><br/>";
				echo "ID: " . $row["ID"] . "<br/>";
				echo "Title: " . $row["song"] . "<br/>";
				echo "Artist: " . $row["artist"] . "<br/>";
				echo "Year: " . $row["year"] . "<br/>";
				echo "Chart: " . $row["chart"] . "<br/>";
				echo "Month: " . $row["month"] . "<br/>";
				echo "Quantity: " . $row["quantity"] . "<br/>";

		}
Many thanks in advance,

Chris

EDIT:

Oh and a couple of things I've done to debug, I've tried:
- Putting in a sleep command in between the two queries
- Running an SQL FLUSH CACHE statement
- Closing the database connection and opening it again

None of these seem to work.

Re: SELECT statement after UPDATE calls old data

Posted: Fri Nov 26, 2010 7:05 am
by Darhazer

Code: Select all

while($row = mysql_fetch_array($result))
And where the $result is defined - it's not the variable the result from last select was assigned to

Re: SELECT statement after UPDATE calls old data

Posted: Fri Nov 26, 2010 6:48 pm
by chricholson
Unbelievable! Had three people staring at that code and no-one spotted it! Thank you very much, works as it should now!