gpt4 book ai didi

php - MAX(user_id) 不工作

转载 作者:行者123 更新时间:2023-11-29 01:42:40 24 4
gpt4 key购买 nike

我想从表中获取最后一个插入 ID,我正在使用 MAX(user_id),但它不起作用(列 user_id 是一个 auto_increment 字段)这里是我的代码

class.php

<?php

class Chat extends Core
{
public function addUser($email) {
$this->query("INSERT INTO `users`(`username`) VALUES ('" . $this->db->real_escape_string($email) . "') ");
$q = "select MAX(user_id) from `users`";
$result = mysql_query($q);
$data = mysql_fetch_array($result);
}
}

}

?>

当我运行这个查询时,我收到了这样的警告消息

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

这是我的 core.php

<?php
class Core {
protected $db,$result;
private $rows;
public function __construct() {
$this->db=new mysqli('localhost','root','','site');
}
public function query($sql)
{
$this->result= $this->db->query($sql);
}
public function rows(){
for($x=1;$x<=$this->db->affected_rows;$x++)
{
$this->rows[]= $this->result->fetch_assoc();
}
return $this->rows;
}
}

?>

谁能帮帮我。谢谢。

最佳答案

看起来你在下面使用 mysqli,所以这会做你想做的事:

public function addUser($email) 
{
$this->query("INSERT INTO `users`(`username`) ...");
$userid = $this->db->insert_id;
// ... etc ...
}

因为 mysqli::$insert_id 是基于 session 的,所以您可以避免导致传递错误标识符的竞争条件。

另请参阅:mysqli::$insert_id

关于php - MAX(user_id) 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com