mysql_insert_id 和我的困境?
我正在尝试获取一行的自动递增列。代码将解释,但基本上我正在尝试将一行插入名为订单的表中,然后我想获取自动递增的数字。 这是我的 PHP。
<?php
$db = DBConnection::connect();
$q = "INSERT INTO orders (customerid, orderdate) VALUES (".$customerid.", CURRENT_TIMESTAMP)";
$ps = $db->prepare($q);
$ps->execute();
$db = null;
echo mysql_insert_id();
?>
在这个阶段,我真正想做的就是回显汽车号码。
这是我的结构
CREATE TABLE `orders` (
`orderid` int(25) NOT NULL AUTO_INCREMENT,
`customerid` int(11) NOT NULL,
`orderdate` date DEFAULT NULL,
PRIMARY KEY (`orderid`),
KEY `orderid` (`orderid`)
)
任何帮助将不胜感激,谢谢:)
I'm trying to get the auto incremented column of a row. The code will explain, but basically I'm trying to insert a row into a table called orders, and then I want to get the auto incremented number.
This is my PHP.
<?php
$db = DBConnection::connect();
$q = "INSERT INTO orders (customerid, orderdate) VALUES (".$customerid.", CURRENT_TIMESTAMP)";
$ps = $db->prepare($q);
$ps->execute();
$db = null;
echo mysql_insert_id();
?>
At this stage all I really want to do is echo out the auto number.
This is my structure
CREATE TABLE `orders` (
`orderid` int(25) NOT NULL AUTO_INCREMENT,
`customerid` int(11) NOT NULL,
`orderdate` date DEFAULT NULL,
PRIMARY KEY (`orderid`),
KEY `orderid` (`orderid`)
)
Any help would be greatly appreciated, thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DBConnection != MySQL
您不能像这样使用不同库中的函数。您必须将
mysql_num_rows()
更改为DBConnection
等效项,或者将DBConnection
内容更改为mysql_ *。
DBConnection != MySQL
You can't use functions from different libraries like that. You must either change
mysql_num_rows()
to theDBConnection
equivalent, or change theDBConnection
stuff tomysql_*
.PDO 与 mysql_* 函数不同。
由于您已经使用了 PDO,因此必须使用 PDO 对象中的
lastInsertId()
方法:PDO is different from
mysql_*
functions.Since you've used PDO, you must use the method
lastInsertId()
from the PDO object:尝试添加
这可能会显示数据库查询生成的任何错误
Try adding
This may show any errors that the database query is generating