我遇到了错误
connected
`Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given`
我知道这意味着提供的 de sql 有问题,但 sql 没问题,我无法让它打印错误。还有其他方法吗?
我的完整代码:
<?php include "inc/config.php"; ?>
<?php include "inc/funciones.php"; ?>
<?php header('Content-Type: text/html; charset=UTF-8'); ?>
<?php
if(mysql_select_db($db_db2)){
echo "connected<br>";
}
$query = 'SELECT * FROM trabajo limit 5';
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query);
try {
while($estado = mysql_fetch_assoc($result)){
$query = 'UPDATE trabajo set estadoActual=(SELECT nombreTarea FROM
tareastrabajo where
numeroEntrada = "'.$estado["numeroEntrada"].'"
AND fechaCompletada is not null
order by fechaCompletada DESC limit 1)
where numeroEntrada="'.$estado["numeroEntrada"].'"';
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query);
}
} catch (Exception $e) {
echo $e->getMessage();
echo "---";
echo mysql_error();
}
?>
如您所见,连接正常,所以没有问题。 我也试过经典的
or die(mysql_error());
但结果相同。
免责声明: 请避免指出有关 mysqli 或 PDO 的答案和评论。 我自己理解这些问题,我们正在迁移它。 与此同时,我们必须处理这个问题。
编辑:谢谢!我正在覆盖 $result。多么蹩脚的方式浪费了我半个上午...
最佳答案
为了扩展 Jon Stirling 的评论,您在 while 循环的第一次迭代中用 UPDATE
语句的结果覆盖了 $result
变量。
因此错误发生在第二次访问条件
while ($estado = mysql_fetch_assoc($result))
来自docs :
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
这个 bool 值被传递回 mysql_fetch_assoc()
导致异常..没有 mysql 错误。
作为对 Geoff Atkin 评论的回应.. mysql_fetch_assoc()
不应正确处理任何返回的行
关于php - mysql "boolean given"没有 mysql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30504643/