0

在我的数据库中,我有一个称为spaj_per自动增量主键的字段。

如何在字段中显示最新插入的值spaj_per。我试过这个。

<?php
    $con = mysql_connect("localhost", "SuperAdmin", "***");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    $db_selected = mysql_select_db("sistem_pengurusan_fail",$con);

    $q = "SELECT MAX(id) AS spaj_per FROM unit_pengambilan"; 
    $result = mysql_query($q); 
    $row = mysql_fetch_array($result); 

?>
<?php echo $row['spaj_per']; ?> 

但它不会起作用。

4

6 回答 6

0

您可以使用

SELECT id FROM mytable ORDER BY id DESC LIMIT 1;

或喜欢

$last_id = mysql_insert_id();
于 2013-01-29T05:20:21.107 回答
0

尝试这个,

$q = "SELECT MAX(spaj_per) AS spaj_per FROM unit_pengambilan"; 
$result =mysql_query($q); 
$row = mysql_fetch_row($result); 

echo $row[0];
于 2013-01-29T05:20:22.083 回答
0

尝试mysql_insert_id

For more reference use following link
http://php.net/manual/en/function.mysql-insert-id.php
于 2013-01-29T05:20:51.753 回答
0

尝试以下

<?
$q = "SELECT MAX(spaj_per) AS last_spaj_per FROM unit_pengambilan"; 
$result =mysql_query($q); 
$row = mysql_fetch_array($result); 

 echo $row['last_spaj_per']; 
?> 
于 2013-01-29T05:22:15.360 回答
0

$row = mysql_fetch_array($result);更改$row = mysql_fetch_assoc($result);并尝试

于 2013-01-29T05:22:33.043 回答
0

使用PHP的mysql_insert_id()函数

于 2013-01-29T05:19:32.470 回答