ProblemHelpPlease Posted July 15, 2009 Share Posted July 15, 2009 I have the following code layout but the UPDATES do not work and the script returns no errors. $result = mysql_query("SELECT * FROM blah WHERE blah = 'blah'");while($row = mysql_fetch_array($result)){mysql_query("UPDATE blah SET blah = 'blah' WHERE blah = '$row['blah']'");} Can I use an UPDATE in the middle of the while like this? Link to comment Share on other sites More sharing options...
smerny Posted July 15, 2009 Share Posted July 15, 2009 trymysql_query("UPDATE blah SET blah = 'blah' WHERE blah = '".$row['blah']."'"); Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted July 15, 2009 Author Share Posted July 15, 2009 I had actually been using $result = mysql_query("SELECT * FROM blah WHERE blah = 'blah'");while($row = mysql_fetch_array($result)){$blah = $row['blah'];mysql_query("UPDATE blah SET blah = 'blah' WHERE blah = '$blah'");} But both the above and your suggestion have no effect. I was really looking to check if using a mysql_query UPDATE inside a while mysql_fetch_array was ok or if this was the cause of it not working.I dont understand why I am not seeing any error messages, if i pull the UPDATE out and run it on its own then it works so I know that the UPDATE itself is ok. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 15, 2009 Share Posted July 15, 2009 It doesn't matter which control structures you put calls to MySQL in, MySQL doesn't check to see what the structure of the PHP code was where the call was made. If it's not running the update queries, then it's probably not looping at all, which means the select statement isn't returning any rows.It's not going to automatically print errors from MySQL, if you want to see those you need to check if an error happened (mysql_query will return false if there was an error), and use the mysql_error function to print the error message. Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted July 15, 2009 Author Share Posted July 15, 2009 I've been echoing the variable on screen so I know that it is finding the correct data and loopingHow would you add the mysql_error function to the code above can you give an example Link to comment Share on other sites More sharing options...
justsomeguy Posted July 15, 2009 Share Posted July 15, 2009 mysql_query("UPDATE blah SET blah = 'blah' WHERE blah = '$blah'") or echo mysql_error(); Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted July 15, 2009 Author Share Posted July 15, 2009 No errors are returned, I can't see any fault with the code but it refuses to work Link to comment Share on other sites More sharing options...
justsomeguy Posted July 15, 2009 Share Posted July 15, 2009 I guarantee the code is doing what you're telling it to do. Print the SQL statements out to see what you're telling it to do. Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted July 16, 2009 Author Share Posted July 16, 2009 Problem SolvedI was combining 2 tables that both contained an ID field so the code was sending to the wrong ID field, I now have and ID field and IDX field. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.