Jump to content

[SOLVED] Updating mysql in WHILE statement.


All4172

Recommended Posts

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

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.