简体   繁体   English

简单MySQL查询中的错误

[英]Error in simple MySQL query

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" “ PHP警告:mysql_fetch_object():提供的参数不是有效的MySQL结果资源”

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\n", mysqli_error($mysqli));
}

mysqli_close($mysqli);

When I var_dump, I get NULL values. 当我var_dump时,我得到NULL值。 Here's what I've tried so far: (1). 到目前为止,这是我尝试过的方法:(1)。 I've executed the SQL query directly in phpmyadmin. 我已经直接在phpmyadmin中执行了SQL查询。 I get a result with a column header of 'Max(id)' (2). 我得到的结果带有'Max(id)'(2)的列标题。 I've tried using mysql_fetch_array(). 我尝试使用mysql_fetch_array()。 I get a log error of: 我收到以下日志错误:

"PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, object given" “ PHP警告:mysql_fetch_array()期望参数1为资源,给定对象”

What am I doing wrong? 我究竟做错了什么?

$row = mysql_fetch_object($res);

should be: 应该:

$row = mysqli_fetch_object($res);

You are trying to fetch an object of mysql, which you haven't set up 您正在尝试获取尚未设置的mysql对象

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). 前缀为mysql_函数来自旧的ext/mysql扩展,该扩展与改进的MySQLi扩展(其函数具有mysqli_前缀)完全分离(并且不兼容)。 You are mixing the two, which won't work. 您正在混合两者,这是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM