简体   繁体   中英

PDO::FETCH_ASSOC not working?

I have this code, basically I need to get the new ID from the most recently insterted row...I have this code and it is not working for me...

$sql = "INSERT INTO `cd_photo` (id, album, date) VALUES (NULL, '" .mysql_real_escape_string($_POST['album']) . "', '" . $date . "')";

if (@mysql_query($sql)) {
$result = $sql->fetch(PDO::FETCH_ASSOC);
$pageid = $result['id'];
echo $pageid;
echo header('Location: newimg.php?id=' .$pageid);
}

Any ideas?

You are mixing two different PHP-MySQL APIs. I suggest you use PDO since mysql_ is phased out. Here is how you'd do it in PDO :

$dsn = 'mysql:dbname=dbname;host=dbhost';
$user = 'dbusername';
$pass = 'dbpass';
$pdo = new PDO($dsn, $user, $pass);

$sql = "INSERT INTO `cd_photo` (album, date) VALUES (:album,:date)"
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':album', $album);
$stmt->bindParam(':date', $date);
$stmt->execute();
$pageid = $pdo->lastInsertId();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM