我正在尝试从Mysql升级到Mysqli,然后将该功能mysql_result升级到mysqli_data_seek。我设法做到了,但是所有统计信息都是错误的,我只计算了1而不是520。

我相信问题仍然存在于mysqli_data_seek($res, 0);中,因为mysql_result就像这样的mysql_result($res, 0, 0);(它有3个参数)。

新密码(Mysqi):

$link  = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = "SELECT name FROM employee";
$res   = mysqli_query($link, $query)

if (mysqli_num_rows($res) == 0) {
    return false;
}

if (mysqli_num_fields($res) > 1) {
    return $this->sqlFetchArray($res);
} else {
    return mysqli_data_seek($res, 0);
}


旧代码(Mysql):

$link  = mysql_connect("localhost", "my_user", "my_password");
$query = "SELECT name FROM employee";
$res   = mysql_query($query);

if (mysql_num_rows($res) == 0) {
    return false;
}

if (mysql_num_fields($res) > 1) {
    return $this->sqlFetchArray($res);
} else {
    return mysql_result($res, 0, 0);
}

最佳答案

mysqli_data_seekmysqli_result的成员,它将指针移至传递给函数的任意行。

the docs

$query = "SELECT Name, CountryCode FROM City ORDER BY Name";
if ($result = $mysqli->query( $query)) {

    /* seek to row no. 400 */
    $result->data_seek(399);

    /* fetch row */
    $row = $result->fetch_row();

    printf ("City: %s  Countrycode: %s\n", $row[0], $row[1]);

    /* free result set*/
    $result->close();
}


您遇到的问题与从mysql更改为mysqli且为described here by tuxedobob有关:


  将旧项目从使用mysql扩展名转换为mysqli
  扩展,我发现最烦人的变化是缺少
  mysqli中相应的mysql_result函数。虽然mysql_result是一个
  通常糟糕的功能,对于获取单个
  结果集中的结果字段值(例如,如果查找一个
  用户的ID)。
  
  mysql_result的行为在这里是近似的,尽管您可能想要
  为它命名而不是mysqli_result以避免思考
  这是一个实际的内置函数。


<?php
function mysqli_result($res, $row, $field=0) {
    $res->data_seek($row);
    $datarow = $res->fetch_array();
    return $datarow[$field];
}
?>



  通过OO接口实现它的工作留给了
  读者。

关于php - mysql_result替换为mysql_data_seek无法正确计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23854434/

10-10 04:47