0

我有一个 6 位数的引脚列表。我想从列表中随机抽取一个 pin 并将其存储在变量 $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 = ?"");

有任何想法吗?

4

2 回答 2

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 绝不是一个坏主意。

于 2013-10-11T18:05:46.243 回答
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));

这是一个非常基本的过程,当然您需要在每个步骤之间进行一些错误检查,以验证每个查询是否成功并且您收到了正确的数据。

于 2013-10-11T18:06:15.723 回答