Reputation: 3
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 :)
Upvotes: 0
Views: 135
Reputation: 3351
PDO is different from mysql_*
functions.
Since you've used PDO, you must use the method lastInsertId()
from the PDO object:
$db->lastInsertId();
Upvotes: 1
Reputation: 863
Try adding
$ps->execute()
or die(mysql_error());
This may show any errors that the database query is generating
Upvotes: -1
Reputation: 324650
DBConnection != MySQL
You can't use functions from different libraries like that. You must either change mysql_num_rows()
to the DBConnection
equivalent, or change the DBConnection
stuff to mysql_*
.
Upvotes: 3