简体   繁体   English

如何在PHP数组中存储和处理查询结果?

[英]How to store and process result of a query in PHP array?

My SQLite query in PHP goes like this: 我在PHP中的SQLite查询如下:

function getTableData($l_name) {
   $db = new PDO("sqlite:../db/mydb.sqlite");
   $results = $db->exec("SELECT * FROM Locations WHERE L_ID = '" . $l_name; . "');";
   .......
}

My questions are: 我的问题是:

  • How can I get an array of Locations based on the $results ? 我如何基于$results获得一系列位置? Could I just write $results[0] or $results[$results.length] to reference first and last records respectively? 我可以只写$results[0]$results[$results.length]分别引用第一条记录和最后一条记录吗?
  • How would I use a foreach loop to loop through all records? 我将如何使用foreach循环遍历所有记录? Could I have foreach ($results as $record) , where $record is supposed to be a single record of Location in the current iteration? 我可以让foreach ($results as $record) ,其中$record应该是当前迭代中Location的单个记录吗?
  • If I were to have a reference to an individual record, say using an array like this records[0] , how would I access this record's field (such as L_Name or L_ID)? 如果要引用单个记录,例如使用类似于records[0]的数组,我将如何访问该记录的字段(例如L_Name或L_ID)?

Thank you. 谢谢。

First of all, your code is just incorrect. 首先,您的代码不正确。 Here goes the proper version: 这里是正确的版本:

$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
$db = new PDO("sqlite:../db/mydb.sqlite", $opt);
$stmt = $db->prepare("SELECT * FROM Locations WHERE L_ID = ?");
$stmt->execute([$l_name]);

And now you can use $stmt variable in place of $results for your second question: 现在,您可以在第二个问题中使用$ stmt变量代替$ results:

How would I use a foreach loop to loop through all records? 我将如何使用foreach循环遍历所有记录? Could I have foreach ($results as $record), where $record is supposed to be a single record of Location in the current iteration? 我是否可以foreach($ results为$ record),其中$ record应该是当前迭代中Location的单个记录?

Yes, you can. 是的你可以。

$stmt = $db->prepare("SELECT * FROM Locations WHERE L_ID = ?");
$stmt->execute([$l_name]);
foreach ($stmt as $row)
{
    // do whatever
}

However, this method won't work with your other requirement. 但是,此方法无法满足您的其他要求。

How can I get an array of Locations based on the $results? 如何根据$ results获得一系列位置信息? Could I just write $results[0] or $results[$results.length] to reference first and last records respectively? 我可以只写$ results [0]或$ results [$ results.length]分别引用第一条记录和最后一条记录吗?

To get a real array, you need PDOStatement::fetchAll()‌​ method. 要获得真正的数组,你需要PDOStatement::fetchAll()‌​方法。 However, to get the last element, the syntax will be different: 但是,要获取最后一个元素,语法将有所不同:

$stmt = $db->prepare("SELECT * FROM Locations WHERE L_ID = ?");
$stmt->execute([$l_name]);
$results = $stmt->fetchAll();
$first = $results[0];
$last = $results[count($results)-1];

foreach ($results as $row)
{
    // do whatever
}

If I were to have a reference to an individual record, say using an array like this records[0], how would I access this record's field (such as L_Name or L_ID)? 如果要引用单个记录,例如使用类似于records [0]的数组,我将如何访问该记录的字段(例如L_Name或L_ID)?

Just reference it. 只是参考一下。 Any time you get a row, you can address its elements this way 任何时候只要一行,就可以用这种方式解决它的元素

echo $row['L_name'];

PDO::exec does not receive the result set returned by the query. PDO::exec不接收查询返回的结果集。 What you want is to use either the PDO::query methods or the PDO::prepare method, both of which give you a PDOStatement to work with. 您想要使用PDO::query方法或PDO::prepare方法,这两种方法都可以使用PDOStatement That in turn is traversable, allowing you to do something like: 这又是可遍历的,使您可以执行以下操作:

$statement = $dbh->prepare("SELECT * FROM Locations WHERE L_ID = :id");
$statement->bindParam(':id', $l_name);
$statement->execute();

// Traverse the result set
while ($row = $statement->fetch()) {
    // Do something with each $row here
}

// Alternatively, you can load all results into an array:
$results = $statement->fetchAll();

Also notice that the example above uses PDOStatement::bindParam to use your variable with the query. 还要注意,上面的示例使用PDOStatement::bindParam在查询中使用变量。 This is vital to preventing SQL injection attacks. 这对于防止SQL注入攻击至关重要。 You can read more about that here: http://php.net/manual/en/security.database.sql-injection.php 您可以在此处阅读有关此内容的更多信息: http : //php.net/manual/en/security.database.sql-injection.php

Here is basic code that loops through the results of your query, allowing you to reference fields in each record returned: 这是循环遍历查询结果的基本代码,使您可以引用返回的每个记录中的字段:

foreach ($db->$query("SELECT * FROM Locations WHERE L_ID = '" . $l_name . "'") as $row)
{
    // $row will contain an array to the current record
    // Address fields using the field name in quotes inside the brackets next to $row
    $variable1 = $row['field1name'];
    $variable2 = $row['field2name'];
}

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

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