我有一个6位数字的列表。我想随机从列表中拉出一个引脚并将其存储在变量$ user_pin中,然后从列表中删除该PIN。
我有以下开始(PDO),但不知道如何存储变量:
选择图钉:
$sth = $this->db->prepare("SELECT available_pins FROM pin_list ORDER BY RAND() LIMIT 0,1 ;");
删除图钉:
$sth = $this->db->prepare("DELETE FROM pin_list WHERE available_pins = ?"");
有什么想法吗?
答案 0 :(得分:1)
您忘记执行查询,您只是在准备它:
try {
// Used this method since you don't have to prepare any variable
$sth = $this->db->query("SELECT available_pins FROM pin_list ORDER BY RAND() LIMIT 0,1 ;");
$pinarray = $sth->fetch();
// Used this method since you can prepare the variable. In this situation it'd be pretty safe even chaining them though
$sth = $this->db->prepare("DELETE FROM pin_list WHERE available_pins = ? LIMIT 0,1");
$sth->execute(array($pinarray['available_pins']));
}
catch(PDOException $e) {
echo "There was an error: " . $e->getMessage();
}
删除命令中也有拼写错误。 DELETE查询中的LIMIT绝不是一个坏主意。
答案 1 :(得分:0)
选择强>:
$sth = $this->db->prepare("SELECT available_pins FROM pin_list ORDER BY RAND() LIMIT 0,1 ;");
$result = $sth->execute();
<强>提取强>:
$user_pin = $result->fetchColumn();
删除强>:
$sth = $this->db->prepare("DELETE FROM pin_list WHERE available_pins = ?");
$sth->execute(array($user_pin));
这是一个非常基本的过程,当然,您需要在每个步骤之间进行一些错误检查,以验证每个查询是否成功以及您是否收到了正确的数据。