简体   繁体   中英

Multiple items from db into an array

I am attempting to pull out some items from a mysql DB and place it into an array. The goal is to have an array so that I can compare 1 variable ($row[0]) to another set variable. Then from that comparison take the second variable ($row[1]) and use that to requery the database.

$timestamp = time();
$drop_dead = mysql_query("SELECT drop_dead, ID FROM eblasts") or die(mysql_error());

    while ($row = mysql_fetch_array($drop_dead, MYSQL_NUM)) {
            $last_time[][] = $row[0];
            $last_time[][] = $row[1];   
    };
    foreach($last_time as $value) {
        if($timestamp > $value){
            $final_email[] = $value;
        };
    };
    print_r($final_email)

Hopefully that makes sense. Anyone have a better solution? This isn't working ...

There are 4 fields in the DB. drop_dead, warning1, warning2 and due_date. Each one of these is a unix time stamp. I need to send an email if that time has past. So I want to look up the drop_dead date from the DB (which will be the latest time an email should be sent) see how it compares to the current time. Then requery the DB and see which email should be sent out warning1, warning2 etc.

Make sense?

Thanks in advance.

-Adam

I think this assignment is causing you trouble:

$last_time[][] = $row[0];
$last_time[][] = $row[1];

Try this instead:

$last_time[] = $row[0];
$last_time[] = $row[1];

You should also initialize $timestamp with a starting value, even if it's just 0.

Did you print $row . It may be an associative array.

Check it first.

$last_time[][] also need an index..i hope.

Try this

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
  }

why not:

    while ($row = mysql_fetch_array($drop_dead, MYSQL_NUM)) {
        $last_time[$row[0]] = $row[1];
    };
    if (array_key_exists($timestamp, $last_time)) {
        $final_email[] = $last_time[$timestamp];
    }

assuming that $row[0] is the timestamp and $row[1] is the email..

assuming the $timestamp is defined earlier in the code.

I generally use array_push() for saving items to an array when it is not associative.

try this:

$drop_dead = mysql_query("SELECT drop_dead, ID FROM eblasts") or die(mysql_error());

while ($row = mysql_fetch_array($drop_dead, MYSQL_NUM)) 
        array_push($last_time, $row);

foreach($last_time as $value)
    if($timestamp > $value[0])
        array_push($final_email, $value[1]);

print_r($final_email)

let me have a guess: at last you want to simply drop some records from eblasts...

if drop_dead is a DATETIME or DATE field:

"DELETE FROM eblast WHERE drop_dead > NOW()"

if drop_dead is an unix_timestamp

"DELETE FROM eblast WHERE FROM_UNIXTIME(drop_dead) > NOW()"

or if you like it complicated ("my" drop_dead id an DATE(TIME) field):

DELETE FROM eblast WHERE ID IN(SELECT ID FROM eblasts WHERE drop_dead > NOW())

if you need to display the deleted IDs:

$query = mysql_query("SELECT ID FROM eblasts WHERE drop_dead > NOW()") or die(mysql_error());
$row = array();
while($row[] = mysql_fetch_array($drop_dead, MYSQL_NUM));
array_pop($row);

echo '<pre>' . print_r($row, true) . '</pre>';

$query = mysql_query(sprintf("DELETE FROM eblast WHERE ID IN(%s)", implode(',', $row))) or die(mysql_error());

and still i could be wrong ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM