All4172 Posted March 13, 2007 Share Posted March 13, 2007 What I'm trying to do, is if a condition is met, then update the mysql db. So I have the following: $server_date = date('U'); $result = mysql_query("SELECT * FROM article WHERE status LIKE 'notlive' AND date>0"); while($row = mysql_fetch_array($result)) { if ($server_date >= $row['date']) { echo "Changed<BR>"; $query = "UPDATE article SET status = 'live' WHERE status='notlive'"; mysql_fetch_array($query);} } I know the condition is met since it displays "Changed" twice, but the UPDATe doesn't appear to go through. Am I able to send an UPDATE in the WHILE statement? Link to comment https://forums.phpfreaks.com/topic/42501-solved-updating-mysql-in-while-statement/ Share on other sites More sharing options...
dymon Posted March 13, 2007 Share Posted March 13, 2007 Hi, why do you use: mysql_fetch_array($query); ?? try: mysql_query($query); Dymon Link to comment https://forums.phpfreaks.com/topic/42501-solved-updating-mysql-in-while-statement/#findComment-206191 Share on other sites More sharing options...
Orio Posted March 13, 2007 Share Posted March 13, 2007 mysql_fetch_array($query); Should be: mysql_query($query); Also, the whole logic of the script confuses me... You could do it this way: <?php $server_date = date('U'); $result = mysql_query("UPDATE article SET status = 'live' WHERE status='notlive' AND date<='$server_date'"); echo mysql_affected_rows()." have been updated!"; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/42501-solved-updating-mysql-in-while-statement/#findComment-206193 Share on other sites More sharing options...
All4172 Posted March 13, 2007 Author Share Posted March 13, 2007 mysql_fetch_array($query); Should be: mysql_query($query); Also, the whole logic of the script confuses me... You could do it this way: <?php $server_date = date('U'); $result = mysql_query("UPDATE article SET status = 'live' WHERE status='notlive' AND date<='$server_date'"); echo mysql_affected_rows()." have been updated!"; ?> Orio. Reason i can't do it that way is I need to retrieve the articles epoch date and compare it to the servers epoch date before I can change its status. So that's the reason I had to break up the queries. There most likely is an easier and cleaner way of doing it but right now its beyond me. Changing to mysql_query($query); did the trick. I just overlooked it. Link to comment https://forums.phpfreaks.com/topic/42501-solved-updating-mysql-in-while-statement/#findComment-206202 Share on other sites More sharing options...
Orio Posted March 13, 2007 Share Posted March 13, 2007 But what you are doing does the same thing I did, unless you add "LIMIT 1" to your query. Both of our scripts update all of the articles you fetched using the select statement from "notlive" to "live", mine just does that to all of the ones that meet the date requirement in your select statement. Orio. Link to comment https://forums.phpfreaks.com/topic/42501-solved-updating-mysql-in-while-statement/#findComment-206205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.