使用PHP无法从MySQL中的表中获取最大ID

时间:2019-03-17 11:57:27

标签: php mysql

你好,我想从我写的MySQL表中获取最大的ID号:

<?php
$servername='localhost';
$username='root';
$password='';
$databasename='myDB';
$con = new mysqli($servername,$username,$password,$databasename);
$max_id= $con->query('SELECT max(id) from myTable');
echo "<pre>";
print_r($max_id);
echo "</pre>";

但是$ max_id成为对象并且输出是这样的

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 1
    [lengths] => 
    [num_rows] => 1
    [type] => 0
)

2 个答案:

答案 0 :(得分:1)

<?php
 $servername='localhost';
 $username='root';
 $password='';
 $databasename='myDB';
 $con = new mysqli($servername,$username,$password,$databasename); 
 $result= $con->query("SELECT max(id) from myTable");
 $max_id= mysqli_fetch_array($result);
 echo "<pre>";
 print_r($max_id);
 echo "</pre>rrr";
?>

答案 1 :(得分:0)

您需要在查询后获取结果。 请在此处阅读手册:https://secure.php.net/manual/en/mysqli.examples-basic.php

在阅读正式文档之前,不要问任何问题。