mysql_fetch_assoc从主键返回数组

时间:2012-09-19 16:03:15

标签: php mysql arrays

我正在尝试使用表单帖子向mySQL表添加行。每行都有一个主要的我正在调用quoteID。提交新表单时,它应将自身添加为表中的一行,其quoteID大于前一个quoteID。它目前看起来像这样:

<?
session_start();
if(!session_is_registered(myusername)){
header("location:login.php");
}
include 'verify.php';
$con = mysql_connect("localhost","user","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("internal", $con);

$previousOrderID = mysql_query("SELECT * FROM sourcingQuote ORDER BY quoteID DESC LIMIT 1"); 
$previousOrderID = mysql_fetch_assoc($previousOrderID);
$newOrderID = $previousOrderID['ID'] + 1;

mysql_close($con);
?>

目前这个表中有4行,引用ID为1,2,3和4.奇怪的是,如果我尝试:

<? echo $previousOrderID; ?><br>
<? echo $newOrderID; ?><br>

输出结果为:

Array

1

为什么$ newOrderID不显示5?和值$ previousOrderID 4?

感谢。

1 个答案:

答案 0 :(得分:3)

您的uniqueid名为quoteID;您正在寻找ID:

$newOrderID = $previousOrderID['ID'] + 1;

试试这个:

$newOrderID = $previousOrderID['quoteID'] + 1;

您目前正在获得1,因为当它没有找到值时,它会返回null,当您向其添加1时,其值为0.

你也可以通过将quoteID字段设为auto_increment来解决这个问题。