douhandie6615 2013-06-13 20:38
浏览 77
已采纳

简单MySQL查询出错

I'm executing a query on my db. I want to fetch the largest value of the table's primary key. I get a null result and an error in my log of:

"PHP Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource"

Here's my code:

$mysqli = new mysqli(MYSQL_HOSTNAME, 'xxx', 'xxx', MYSQL_DATABASE);
if (mysqli_connect_errno()) 
    exit();

$sql = "SELECT MAX(id) FROM `Invoice`";
//$sql = "SELECT id FROM `invoice`";
$res = mysqli_query($mysqli, $sql);

var_dump(get_object_vars($res));

if ($res) {
    $row = mysql_fetch_object($res);
    var_dump($row);
    //echo $row->MAX(id);    
}  else {
    printf("Could not retrieve records: %s
", mysqli_error($mysqli));
}

mysqli_close($mysqli);

When I var_dump, I get NULL values. Here's what I've tried so far: (1). I've executed the SQL query directly in phpmyadmin. I get a result with a column header of 'Max(id)' (2). I've tried using mysql_fetch_array(). I get a log error of:

"PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, object given"

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dongshang1979 2013-06-13 20:41
    关注

    Functions prefixed mysql_ are from the old ext/mysql extension, which is completely separate (and incompatible) with the improved MySQLi extension (whose functions have the mysqli_ prefix). You are mixing the two, which won't work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端