将 SQL 结果转换为 PHP 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17056349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert SQL results into PHP array
提问by Jonathan Plumb
I'm fairly new to PHP and I've been looking around and can't seem to find the specific answer I'm looking for.
我对 PHP 相当陌生,我一直在环顾四周,似乎无法找到我正在寻找的具体答案。
I want to make a SQL query, such as this:
我想进行 SQL 查询,例如:
$result = mysqli_query($connection, $command)
if (!$result) { die("Query Failed."); }
// Create my array here ... I'm thinking of maybe having to
// make a class that can hold everything I need, but I dunno
while($row = mysqli_fetch_array($result))
{
// Put the row into an array or class here...
}
mysqli_close($connection);
// return my array or class
Basically I want to take the entire contents of the result and create an array that I can access in a similar fashion as the row. For example, if I have a field called 'uid' I want to be able to get that via myData['uid']. I guess since there could be several rows, maybe something more like myData[0]['uid'], myData[1]['uid'], etc.
基本上我想获取结果的全部内容并创建一个数组,我可以以与行类似的方式访问该数组。例如,如果我有一个名为“uid”的字段,我希望能够通过 myData['uid'] 获取该字段。我想因为可能有几行,也许更像是 myData[0]['uid']、myData[1]['uid'] 等。
Any help would be appreciated.
任何帮助,将不胜感激。
回答by karthikr
You can do:
你可以做:
$rows = [];
while($row = mysqli_fetch_array($result))
{
$rows[] = $row;
}
回答by BlitZ
You might try to use mysqli_result::fetch_all()
for arrays:
您可以尝试使用mysqli_result::fetch_all()
数组:
$result = mysqli_query($connection, $command)
if (!$result) { die("Query Failed."); }
$array = $result->fetch_all();
$result->free();
mysqli_close($connection);
NOTE:This works with MySQLNDonly.
注意:这仅适用于MySQLND。
For class, you might try to use something like this:
对于课堂,您可以尝试使用以下内容:
$result = mysqli_query($connection, $command)
if (!$result) { die("Query Failed."); }
while($model = $result->fetch_assoc()){
// Assuming ModelClass is declared
// And have method push() to append rows.
ModelClass::push($model);
}
$result->free();
mysqli_close($connection);
OR this:
或这个:
// Base Model - abstract model class.
class ModelClass extends BaseModel {
// constructor
public function __construct(mysqli &$dbms){
// $this->dbms is MySQLi connection instance.
$this->dbms = &$dbms;
// $this->models is buffer for resultset.
$this->models = array();
}
// destructor
public function __destruct(){
unset($this->dbms, $this->models);
}
public function read(){
$result = $this->dbms->query($command);
if($this->dbms->errno){
throw new Exception($this->dbms->error, $this->dbms->errno);
}
$this->models = $result->fetch_all();
$result->free();
}
}
回答by robertjoep
//object oriented style mysqli
//connect to your db
$mysqli = new mysqli("host", "user", "password", "dbname");
$result = $mysqli->query("SELECT * FROM `table`");
//use mysqli->affected_rows
for ($x = 1; $x <= $mysqli->affected_rows; $x++) {
$rows[] = $result->fetch_assoc();
}
//this will return a nested array
echo "<pre>";
print_r($rows);
echo "</pre>";
edit this and put it on a class and just call it anytime you're going to make a query with your database.
编辑它并将其放在一个类中,只要您要对数据库进行查询,就可以随时调用它。