[英]Get last record in mysql
Using this php code: 使用此php代码:
try{
$dbh = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// INSERT CLEAN DATA INTO TABLE…
$sth = $dbh->prepare("
INSERT INTO Fan(fanNm,fanEmail,fanPass,fanDynamSalt)
VALUES('$userName','$userEmailAddress','$userPassword','$dynamSalt')"
);
$sth->execute();
////////////////////////////////////////////////////////////////////
## Set Session Var for this PK ID in Fan table that is being created ##
////////////////////////////////////////////////////////////////////
$_SESSION['newUserSessID'] = mysql_insert_id();
echo "<strong style='color:#fff;'>".$_SESSION['newUserSessID']."</strong>";
} //try
catch(PDOException $e){
echo "Oops, We're experiencing an error.";
file_put_contents('/PDODBConnectionErrors.txt', $e->getMessage(), FILE_APPEND);
} //catch
It inserts FINE into the database, but I am using echo "<strong style='color:#fff;'>".$_SESSION['newUserSessID']."</strong>";
它将FINE插入数据库,但是我正在使用echo "<strong style='color:#fff;'>".$_SESSION['newUserSessID']."</strong>";
to echo out that value and it ALWAYS returns a zero. 回显该值,它总是返回零。
Even if I run : 即使我跑:
SELECT LAST_INSERT_ID( )
FROM Fan
LIMIT 0 , 30
It gives me output like 它给我输出像
0
0
(since there is two rows in database table) (因为数据库表中有两行)
Anyone? 任何人?
You should not use mysql_insert_id
as that isn't part of PDO (which is what you're using to interact with the database in this instance). 您不应该使用mysql_insert_id
,因为它不是PDO的一部分(在这种情况下,PDO就是与数据库交互的对象)。
Instead use PDO::lastInsertId()
: 而是使用PDO::lastInsertId()
:
$_SESSION['newUserSessID'] = $dbh->lastInsertId();
More info: http://www.php.net/manual/en/pdo.lastinsertid.php 更多信息: http : //www.php.net/manual/zh/pdo.lastinsertid.php
You're mixing PDO
with generic MySQL
functions. 您正在将PDO
与通用MySQL
函数混合使用。 The problem is the mysql_last_insert
has no resource, and so it returns 0 for false. 问题是mysql_last_insert
没有资源,因此对于false返回0。 Don't mix PDO
with generic MySQL
functions. 不要将PDO
与一般的MySQL
函数混合使用。
To get the last insert id in PDO, do this: $dbh->lastInsertId()
要获取PDO中的最后一个插入ID,请执行以下操作: $dbh->lastInsertId()
http://php.net/manual/en/pdo.lastinsertid.php http://php.net/manual/zh/pdo.lastinsertid.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.