我遇到了mysqli_query的奇怪行为问题。有一个foreach循环来更新表行,但最后一个循环的更新不会写入数据库(所有其他更新都会成功写入)。没有错误,mysqli_affected_rows会返回1。代码一直持续到最后,由mysqli_query之后的echo语句确认。
我试图通过只有一个元素和同样问题的数组循环。
这里发生了什么?有没有人遇到同样的问题?
好的......这是代码的重要部分,整个代码是不可能的,因为最多允许30000个字符。我也回应了sql-string,一切都很完美。但正如我所说的最后一个循环的更新(循环执行的最后一次)不会写入MySQL-DB。
$sql_getRelations = "SELECT id_machine, device_id, serial_number, variant_id, company_id, last_file FROM view_machines "
. "WHERE company_id NOT IN ('" . implode( '\',\'' , $btc_companies) . "') "
// . "AND id_machine = 158 "
. "AND device_type_id = 4 AND machine_status = 'active' "
. "ORDER BY id_machine ASC;";
$result_relations = mysqli_query($db_ed, $sql_getRelations);
$relations_arr = mysqli_fetch_all($result_relations, MYSQLI_ASSOC);
mysqli_free_result($result_relations);
foreach($relations_arr as $machine){
//...some other code here
$sql_update_device = "UPDATE device SET last_file = '" . $str_max_ts . "' WHERE id_device = " . $machine['device_id'] . ";";
$update_device = mysqli_query($db_ed, $sql_update_device);
if (!$update_device) {
error_log("\r\n" . date("Y-m-d H:i:s") . ': !!!!Updatefehler device: ' . $machine['device_id'] . ": " . mysqli_error($db_ed), 3, "C:/xampp/htdocs/cronjob/error_log.txt");
}
echo $machine['id_machine'] . ', ';
}
mysqli_close($db_ed);
答案 0 :(得分:1)
我会对文本文件执行如下调试。在外部循环中楔入您的数据集合然后将其重新调出并在内部块上调零。分而治之的策略。
我本来试着把你的脑袋放到你的电子邮件中,但它是675行的一个foreach块:p
PHP:
<?php
//mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
mysqli_report(MYSQLI_REPORT_ALL);
error_reporting(E_ALL); // report all PHP errors.
ini_set("display_errors", 1);
echo "start<br/>";
// $file="/home/nate/debug123.txt";
$file="c:\\nate\\debug123.txt";
date_default_timezone_set('America/New_York');
$debugStr=date('Y-m-d H:i:s').": Start";
file_put_contents($file, $debugStr."\n", FILE_APPEND | LOCK_EX);
try {
$mysqli= new mysqli('localhost', 'theUser', 'thePassword', 'theDB_Name');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
echo "I am connected and feel happy.<br/><br/>";
$query = "INSERT INTO `t921`(`thing`,`uniqueNum`,`views`) values ('aaa',577,0)"; // change that 577 each time else it fails unique
echo $query."<br>";
$mysqli->query($query);
$debugStr=date('Y-m-d H:i:s').": Affected rows: " .$mysqli->affected_rows.", Insert id: ".$mysqli->insert_id;
echo $debugStr."<br><br>";
file_put_contents($file, $debugStr."\n", FILE_APPEND | LOCK_EX);
$query = "UPDATE `t921` SET `views`=`views`+1 WHERE id=1";
echo $query."<br>";
$mysqli->query($query);
$id=1;
$debugStr=date('Y-m-d H:i:s').": UPDATE id=".$id.", Affected rows: " . $mysqli->affected_rows;
echo $debugStr."<br><br>";
file_put_contents($file, $debugStr."\n", FILE_APPEND | LOCK_EX);
$query = "UPDATE `t921` SET `views`=`views` WHERE id=1";
echo $query."<br>";
$mysqli->query($query);
$id=1;
$debugStr=date('Y-m-d H:i:s').": UPDATE id=".$id.", Affected rows: " . $mysqli->affected_rows;
echo $debugStr."<br><br>";
file_put_contents($file, $debugStr."\n", FILE_APPEND | LOCK_EX);
$mysqli->close();
} catch (mysqli_sql_exception $e) {
throw $e;
}
?>
示例日志文件(debug123.txt):
2016-09-19 18:23:57: Start
2016-09-19 18:23:57: Affected rows: 1, Insert id: 27
2016-09-19 18:23:57: UPDATE id=1, Affected rows: 1
2016-09-19 18:23:57: UPDATE id=1, Affected rows: 0
请注意,上次更新stmt意味着不会更改因此受影响的数据= 0
架构:
create table t921
( id int auto_increment primary key,
thing varchar(100) not null,
uniqueNum int not null,
views int not null,
unique key(uniqueNum)
);