mercredi 5 août 2015

run php script across all records in mysql table

I have a script that needs to run across all records in a mysql table with an 'id' auto incremented field.

As you can see below I have an array, then an if statement within this array, then a second array within the if statement.

I thought the below might work, looping through the table id's one by one, but the problem is at $array2.

I wanted $array2 to use the :id from the first array, looping through each record in the table and running the below statement, however I see an error.

PHP error: 'Invalid parameter number: parameter was not defined' at $array2.

How can I get the id field from each record to run the statements?

The below code needs to run across every record in the table, does anyone know a different way to do this?

$array = $pdo->prepare("select * from table1 where live = 1");
$array->execute();

foreach ($array as $print) {

            $id = $print['id'];

if($todays_date > $stop_date && $live == 1){

$array2 = $pdo->prepare("SELECT * FROM table2 WHERE product_id = :id ORDER BY field DESC LIMIT 1");
$array2->execute(array(':id' => $id));

foreach($array2 as $print2){

$field1 = $print2['field1'];
$field2 = $print2['field2'];    

$query = $pdo->prepare("UPDATE table SET live = 0, field1 = :field1, field2 = :field2 where id = :id");
$query->execute(array(':field1' => $field1, ':field2' => $field2, ':id' => $id));

}
}

if($todays_date > $stop_date && $live == 0){}

}

Aucun commentaire:

Enregistrer un commentaire