SELECT statement after UPDATE calls old data

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
chricholson
Forum Newbie
Posts: 2
Joined: Fri Nov 26, 2010 4:55 am

SELECT statement after UPDATE calls old data

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: SELECT statement after UPDATE calls old data

Post 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
chricholson
Forum Newbie
Posts: 2
Joined: Fri Nov 26, 2010 4:55 am

Re: SELECT statement after UPDATE calls old data

Post by chricholson »

Unbelievable! Had three people staring at that code and no-one spotted it! Thank you very much, works as it should now!
Post Reply