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条)

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理