I have 2 databases, SYSTEM and CUSTOMER. In SYSTEM there is a stafflist table with usernames. Each table in CUSTOMER is a customers name.
What I am trying to do is foreach table in Customer, foreach 'username in system.stafflist', ALTER TABLE '$table' ADD '$username'
Here is my code:
$sql3 = "SELECT username from system.stafflist";
$result3 = $conn->query($sql3);
$sales = array();
while($row3 = $result3->fetch_array()) {
$sales[]="x".$row3['username'];
}
$sql = "SHOW TABLES from customer";
$result = $conn2->query($sql);
$table = array();
$i=0;
while($row = $result->fetch_array()) {
$table[] = $row[$i];
}
var_dump ($table);
var_dump ($sales);
foreach ($table as $table){
foreach ($sales as $sales){
$sql2 = "ALTER TABLE $table ADD $sales VARCHAR( 12 );";
if ($conn2->query($sql2) === TRUE) {} else {echo "Error: " . $sql2 . "<br>" . $conn2->error;}
}
}
The var_dump shows two proper arrays:
array (size=62)
0 => string 'rita' (length=4)
1 => string 'sadk' (length=4)
2 => string 'sand' (length=4)
3 => string 'sany' (length=4)
4 => string 'sbdk' (length=4)
5 => string 'sdkg' (length=4)
array (size=54)
0 => string '0007' (length=4)
1 => string '0006' (length=4)
2 => string '0005' (length=4)
3 => string '0004' (length=4)
4 => string '0003' (length=4)
But there are two errors:
Error: ALTER TABLE rita ADD x2304 VARCHAR( 12 ); Duplicate column name 'x2304'
And
Warning: Invalid argument supplied for foreach() <--the $sales foreach
If I comment out the $sales foreach loop and change my ALTER statement column name to 'abc' instead of $sales then I can successfully add 'abc' to each table.
What is causing the $sales foreach to give errors?