我有我编写的以下 PHP 函数。它是创建一个新的数据库表并向它和另一个表插入一些值。
function createTable($tablename, $mins){
$con=mysqli_connect("localhost","****","****","*****");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$tableCreate = "CREATE TABLE rates_{$tablename} (
Weight int(11),
CBMMin double,
CBMMax double,
Min double
)";
$queryResult = mysqli_query($con, $tableCreate);
if ($queryResult === TRUE) {
print "<br /><br />Table Created";
} else {
print "<br /><br />No TABLE created. Check " . mysqli_error($con);
}
//Echo/print stop working here but below query does fire
$queryResult = mysqli_query($con, "INSERT INTO Custom_Rates (TableName) VALUES ('rates_{$tablename}');");
//everything below here does not
$rows = array(
array('weight' => 1000, 'cbm_min' => 0.1, 'cbm_max' => 2.3 ),
array('weight' => 1500, 'cbm_min' => 2.31, 'cbm_max' => 3.5 ),
array('weight' => 2000, 'cbm_min' => 3.51, 'cbm_max' => 4.6 ),
array('weight' => 2500, 'cbm_min' => 4.61, 'cbm_max' => 5.75 ),
array('weight' => 3000, 'cbm_min' => 5.75, 'cbm_max' => 6.9 ),
);
foreach ($rows as $row) {
$queryResult = mysqli_query($con, "INSERT INTO 'rates_".$tablename."' (Weight, CBMMin, CBMMax) VALUES (".$row['weight'].",".$row['cbm_min'].",".$row['cbm_max'].");");
if ($queryResult === TRUE) {
print "<br /><br />Row Created";
} else {
print "<br /><br />No Row created. Check " . mysqli_error($con);
}
}
似乎 for each 循环简单不会被触发,因为 true 或 false if 语句都不会被触发,因为没有打印任何内容。我添加了额外的打印语句来调试最后实际执行的操作,似乎在
$queryResult = mysqli_query($con, "INSERT INTO Custom_Rates (TableName)
VALUES ('rates_{$tablename}');");
行没有其他任何事情发生(而且我知道这个查询有效,因为我可以在费率表中看到日期)。
关于什么可能阻止 foreach 工作的任何想法?