mysqli $result回显良好,如果直接输入数据库(使用 xampp)。PHP 版本 7.1.4)。
但是,在脚本中,它似乎总是返回一个结果! - 有点..
TEST1 被触发,但 TEST 2?? 和 TEST3 未触发。
我现在的测试应该只触发 TEST3——确实可以直接输入!
那么什么是坏的??
-谢谢
for ($x=0; $x<count($TransArray); $x++) // for each entry in the array
{
$enterID = (string)$TransArray[$x][0];
$enterC = (string)$TransArray[$x][1];
$enterQ = (string)$TransArray[$x][2];
// Check to see if the CQID is in the fler_datatable with a matching language
// If so then we need to 'update'
// if not then we need to 'insert'
// NOTE : ‘cqid’ and ‘language’ make a unique value in the table
$result = mysqli_query($con,"SELECT rowID FROM fler_datatable
WHERE cqid='$enterID' AND itemid='SYS_DATA_TRANS' AND language='$LANG' LIMIT 1");
if ($result) // found it!
{
echo"<BR>TEST1";
while ($row = $result->fetch_assoc())
{
echo"<BR>TEST2";
$SetRow = $row['rowID'];
echo"<BR>Updatting translation entry on row ".$SetRow.".";
mysqli_query($con, "UPDATE fler_datatable SET class='$enterC', question='$enterQ' WHERE rowID='$SetRow'");
}
}
else // Not found
{
echo"<BR>TEST3";
echo"<BR>Adding new translation entry .";
mysqli_query($con, "INSERT INTO fler_datatable (cqid, class, question, itemid, language, date)
VALUES ('$enterID', '$enterC', '$enterQ' , 'SYS_DATA_TRANS', '$LANG', '$date')");
}
}
是的,这是因为您使用非OOP mysqli来执行查询并获取结果(这是真的),但是在访问它时,您使用的是基于OOP的mysqli。
因此,自始至终使用一种方法,即获取行作为$row = mysqli_fetch_assoc($result)
。
一个朋友有答案=3
if ($result) {}
else {}
是测试"无结果"的错误方法。
if ($result) 为 true,即使它找不到行。
要使用的正确代码是...
if ($result->num_rows > 0 ) {} // found
else {} // Nothing found