我试图检索已插入数据库的记录。我使用MAX(id)
执行此操作,但它始终返回null
。我已尝试在phpmyadmin
中运行查询,但它确实有效。我已尝试过我在网站上找到的其他方法,但都没有。非常感谢帮助。
我的代码:
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO tblgroups (cid, name) values(?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array("1", $name));
$sql = "SELECT MAX(gid) AS gid FROM tblgroups";
$q = $pdo->prepare($sql);
$data = $q->fetch(PDO::FETCH_ASSOC);
$gid = $data['gid'];
$sql = "INSERT INTO tblcategories (cid, gid, name, text_color, bg_color) values(?, ?, ?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array("1", $gid, $name, "#FF0000", "#FFFFFF"));
$sql = "INSERT INTO tblusers (username, password, gid) values(?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($name, md5("password"), $gid));
$sql = "SELECT MAX(uid) AS uid FROM tblusers";
$q = $pdo->prepare($sql);
$data = $q->fetch(PDO::FETCH_ASSOC);
$uid = $data['uid'];
$sql = "INSERT INTO tbluser_groups (gid, uid) values(?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($gid, $uid));