PHP - 从Query创建数组

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (116)

我需要用查询中的值填充此数组。

$articles = array(
    new article("1", "Example item #1", "4.00"),
    new article("1", "Another thing", "3.50"),
    new article("1", "Something else", "1.00"),
    new article("1", "A final item", "4.45"),
); 

$sql="SELECT quantity, desc, curr FROM sales";

我需要一个类的数组中的文章。我该怎么做呢?

提问于
用户回答回答于

试试这个提供的文章是班级名称;

<?php
$dbcon = mysqli_connect("your_host","your_user","your_password","your_db");

// Check connection errors
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//Run your query using the connection 
$sql="SELECT quantity, desc, curr FROM sales";
$result = mysqli_query($dbcon,$sql);

//Check for any error from the query
if(!$result) echo mysqli_error($dbcon);

//Read the results
$rows = mysql_fetch_assoc($result);

//Now create the array you want using value from each row
$articles = array();
while ($rows){
$articles[] =  new article($rows['quantity'], $rows['desc'], $rows['curr']);
}
?>

请原谅我生锈的打字......仍然试图在移动应用程序上这样做

用户回答回答于

我不知道你到底想要什么?

也许这会给你带来一些价值。

foreach($your_query_result AS $key => $value){
    //if your query result is in object
    $articles = array($value->quantity, $value->desc, $value->curr);

    //if your query result is in array
    $articles = array($value['quantity'], $value['desc'], $value['curr']);
} 

扫码关注云+社区

领取腾讯云代金券