【问题标题】:Fatal error: Call to undefined function mysqli_result()致命错误:调用未定义函数 mysqli_result()
【发布时间】:2013-07-16 10:49:56
【问题描述】:

当我尝试将旧 sql 切换为 sqli 时,有人可以告诉我为什么这不起作用:

$query = "SELECT * FROM `product_category`";
$result = mysql_query($query, $connect) or die("could not perform query: " . mysql_error());
$num_rows = mysql_num_rows($result);

for ($i=0; $i < $num_rows; $i++)
{
    $ID = mysql_result($result,$i,"ID");
    $name = mysql_result($result,$i,"name");
    $description = mysql_result($result,$i,"description");

到:

$query = ("SELECT * FROM `product_category`");
$result = mysqli_query($connect, $query) or die("could not perform query");
$num_rows = mysqli_num_rows($result);

for ($i=0; $i < $num_rows; $i++)
{
    $ID = mysqli_result($result, "ID");
    $name = mysqli_result($result,$i,"name");
    $description = mysqli_result($result,$i,"description");`

它一直给我一个错误:“致命错误:调用未定义的函数 mysqli_result()”

【问题讨论】:

  • mysqli_error() 的电话在哪里?您需要将连接变量作为参数传递给它。
  • 没有mysqli_result()函数。您可以提供自己的来模拟mysql_result() 函数的行为,但您并不想这样做。最好使用较新的模型,只需调用 mysqli_fetch_assoc() 函数即可。并且不需要获取行数和 for 循环。只需使用一个while循环。 mysqli_fetch_assoc() 函数将在没有更多行要获取时返回 FALSE,从而使您退出循环。请参阅 Marc B 的答案。
  • 当你使用一个开源项目并且他们只从一列中选择一个值并且你必须修复它,因为它到处都坏了......该死的 Salazar!

标签: php mysql database mysqli database-connection


【解决方案1】:

不要使用这种代码。这是非常低效的。请改用mysqli_fetch_assoc()

while($row = mysqli_fetch_assoc($result)) {
   $id = $row['ID'];
   $name = $row['name']; 
   etc..
}

一个 SINGLE 数据库操作,而不是您尝试执行的 3+ 个。

【讨论】:

  • +1。要真正回答 OP 提出的第一个问题,“告诉我为什么这不起作用”......问题中的代码不起作用的原因是它调用了不存在的 mysqli_result() 函数。该函数不存在。
  • 是的,因为它是 mysql 库中的一个白痴函数。
  • 嘿! :) 我们中的一些白痴碰巧喜欢我们可以使用的白痴功能,但我们却无法理解所有发生在窗帘后面的 dem highfalutin hijinx dat。我只想要我的白痴场值! (我的孩子,我的孩子)(我见过 mysqli_result 函数 的实现)。
  • 是的,另一个mysqli函数的优秀限制。这是要走的路。
【解决方案2】:
if (!function_exists('mysqli_result')) {
  function mysqli_result($res, $row, $field=0) {
    $res->data_seek($row);
    $datarow = $res->fetch_array();
    return $datarow[$field];
  }
}

你可以创建这个函数。

【讨论】:

  • 当您需要重新启动并运行旧代码时,这是一个有用的答案。您可以使用function_exists() 仅在需要时创建函数。
  • 处理旧代码的非常好的方法,这些旧代码需要升级到更现代的 mysqli 标准,但缺少代码的重写,这是 Marc B 在这里展示的非常好的和唯一的工作方式。
猜你喜欢
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 2012-07-18
相关资源
最近更新 更多