0

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\n", 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?

4

2 回答 2

1

$row = mysql_fetch_object($res);

应该:

$row = mysqli_fetch_object($res);

您正在尝试获取尚未设置的 mysql 对象

于 2013-06-13T20:42:12.973 回答
0

带前缀mysql_的函数来自旧扩展,它与改进的 MySQLi 扩展(其函数具有前缀)ext/mysql完全分离(且不兼容)。mysqli_您将两者混合,这是行不通的。

于 2013-06-13T20:41:15.673 回答