I got a Bash/PHP script that retrieves records from a table called 'assess_2012' and stores records that need to be written to a table called 'assess_2012_err' in a multidimensional array:
#!/usr/bin/php -q
<?php
$host = "localhost";
$user = "username";
$pass = "password";
$name = "reassess";
$conn = mysqli_connect($host, $user, $pass, $name) OR die ("Could not connect to database: " . mysqli_error($conn) . "\n");
$q = "SELECT su_id, ass_date, ind_d FROM assess_2012";
$r = mysqli_query ($conn, $q);
if ($r) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
// Check for errors:
if ($row['ind_d'] == 'Y') {
// Add to the array:
$sql[] = array('su_id' => $row['su_id'], 'err_code' => 1);
}
}
var_dump($sql);
}
else {
// SELECT query failed:
echo "Error: " . mysqli_error($conn) . "\n";
}
?>
The 'var_dump' looks like this (shortened version - the actual query returns hundreds of records):
array(1) {
[0]=>
array(2)
["su_id"]=>
string(1) "5"
["err_code"]=>
int(1)
}
[1]=>
array(2) {
["su_id"]=>
string(4) "1492"
["err_code"]=>
int(1)
}
}
What I can't figure out is how I can use the array to produce a query like this:
INSERT INTO assess_2012_err (su_id, err_code) VALUES (5, 1), (1492, 1)
INSERT INTO assess_2012_err (su_id, err_code) VALUESand append it with actual values in a loop