简体   繁体   English

在PDO中输出选择查询

[英]Outputting a Select Query in PDO

I'm used to using BindParam() and since this is a SELECT query Param's are not related. 我习惯使用BindParam() ,因为这是一个SELECT query Param无关。 Basically I'm trying to make a notification system that will check the database and if any of the rows' status is '0' then it would output all the rows information. 基本上,我试图创建一个通知系统来检查数据库,并且如果任何行的状态为“ 0”,那么它将输出所有行信息。

I have the following columns: id, api, request, apikey, apiemail, keyauth, ip, dateandtime, status . 我有以下几列: id, api, request, apikey, apiemail, keyauth, ip, dateandtime, status

How would I make PDO put all the information it gathers from all rows with status='0' and put them into usable variables like: $id, $api, $request ? 我将如何让PDO将所有从status='0'行中收集的所有信息放入可用变量中,例如: $id, $api, $request Of course there could be more then one row with status='0' so maybe have the variables arrays and output like $id[0], $id[1] etc 当然, status='0'一行以上,因此可能具有变量数组和输出,例如$id[0], $id[1]

PDO: PDO:

<?php

include('/cdn/global/db.php');

$opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
$dsn = "mysql:host=$host;dbname=$dbname";

$DBH = new PDO($dsn, $username, $password, $opt);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$STH = $DBH->prepare("SELECT id, api, request, apikey, apiemail, keyauth, ip, dateandtime, status FROM apirequests WHERE status = 0");

$STH->execute();

echo "<p>The link is now in Queue, An admin will check the link soon!</p>";


# close the connection  
$DBH = null;

######

?>

You can fetch them as objects(Also order by a field if you want the results in a specific order): 您可以将它们作为对象来获取(如果希望结果按特定顺序排列,也可以按字段顺序排列):

$STH = $DBH->prepare("SELECT id, api, request, apikey, apiemail, keyauth, ip, dateandtime, status FROM apirequests WHERE status = 0 ORDER BY id");
$STH->execute();
if($STH->rowCount()){
 while($row = $STH->fetch(PDO::FETCH_OBJ)){
  #Perform whatever operation you need on a single row
  echo "$row->id, $row->api, $row->request, $row->apikey, $row->apiemail\n";
 }
}

put them into usable variables like: $id, $api, $request etc 将它们放入可用变量中,例如:$ id,$ api,$ request等

There is no point in doing that. 这样做是没有意义的。 $row['id'] is no less usable but WAY more flexible. $row['id']同样有用,但更灵活。 Although you can use extract() function to assign array members to corresponding variables. 尽管可以使用extract()函数将数组成员分配给相应的变量。

of course there could be more then one row with status='0' so maybe have the variables arrays and output like $id[0], $id[1] etc 当然,状态可能为'0'的一行以上,因此可能具有变量数组和输出,例如$ id [0],$ id [1]等

This would make even less sense and nobody is doing it this way. 这将变得毫无意义,而且没有人这样做。 Anyways, PDO won't make such variables for you. 无论如何,PDO不会为您创建此类变量。 Instead, array of rows have to be used, like shown in other answers. 相反,必须使用行数组,如其他答案所示。

$value = 0;     
$STH = $DBH->prepare("SELECT id, api, request, apikey, apiemail, keyauth, ip, dateandtime, status FROM apirequests WHERE status = ?");
$STH->execute(array($value));
 $variable[] = $STH->fetchAll(PDO::FETCH_ASSOC);    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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