资源 ID#5 PHP MySQL 错误


Resource ID#5 PHP MySQL Error

我有这个代码:

<?php
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);
     if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }
     mysql_select_db(DB_NAME, $con);
 $result = mysql_query("SELECT SUM(colname) FROM profits");
 $total = reset (mysql_fetch_assoc($result));
 mysql_close($con);
 echo $total;
 ?>

它输出的资源 ID #5。 有什么帮助吗? 提前感谢!

不要使用这样的reset() - 如果您需要单个值,只需使用 list()

$result = mysql_query("SELECT SUM(colname) FROM profits");
list($total) = mysql_fetch_array($result);

reset()在关联数组上无法按预期工作:https://bugs.php.net/bug.php?id=38478

您正在尝试回显与 echo $total; 的关联数组。相反,只需回显第一个(可能仅基于您的查询)项。

$row = mysql_fetch_assoc($result);
echo $row[0];