添加数据库结果数组 [英] Adding database results to array

查看:145
本文介绍了添加数据库结果数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个精神空白,不能为我的生命找出一个解决方案。

I am having a mental blank here and cannot for the life of me figure out a solution.

我的情况是,我在PHP和MySQL AM程序。我有一个数据库表中返回的结果为特定订单ID。查询最多不超过4阶行至少有1排的可以退货。

My scenario is that I am programming in PHP and MySQL. I have a database table returning the results for a specific orderid. The query can return a maximum of 4 rows per order and a minimum of 1 row.

下面是我多么想返回结果的图像。

Here is an image of how I want to return the results.

我有存储在一个名为订单表中的所有ORDERDETAILS(名称,地址)等。
我把所有的包存储在名为表序包。

I have all the orderdetails (Name, address) ect stored in a table named "orders". I have all the packages for that order stored in a table named "packages".

我需要的是使用一个循环,我需要能够访问数据库结果的每个特定元素来(IE包装1,itemstype1 1,包装,itemtype2)等

What I need to do is using a loop I need to have access to each specific element of the database results (IE package1, itemstype1, package2, itemtype2) ect

我使用这样的查询,试图得到的物品只是号保持:

I am using a query like this to try and get hold of just the "number of items:

$sql = "SELECT * FROM bookings_onetime_packages WHERE orderid = '".$orderid."' ORDER BY packageid DESC";
$total = $db->database_num_rows($db->database_query($sql));

$query = $db->database_query($sql);

$noitems = '';
while($info = $db->database_fetch_assoc($query)){
$numberitems = $info['numberofitems'];

for($i=0; $i<$total; $i++){

$noitems .= $numberitems[$i];

}

}
print $noitems;

我需要访问每个特定的元素,因为我他们需要创建一个使用FPDF填写PDF模板。

I need to have access to each specific element because I them need to create fill out a pdf template using "fpdf".

我希望这是有道理的。任何方向将大大AP preciated。

I hope this makes sense. Any direction would be greatly appreciated.

推荐答案

您应该做这样的事情:

$data = array();
while($row = $db->database_fetch_assoc($query))
{
    $data[] = $row;
}

现在$ data是一个数组,其中每个元素是查询结果的一行。

Now $data is an array where each element is a row of the query result.

然后就可以访问该行作为$数据[0],$数据[1],并作为$行内的元素数据[1] ['包'],$数据[0] ['项目类型'] ,因为每一行是关联数组。

Then you can access the rows as $data[0], $data[1], and the elements within the rows as $data[1]['package'], $data[0]['itemtype'], because each row is an associative array.

您可以使用count($数据)返回的行数。

You can get the number of rows returned using count($data).

这篇关于添加数据库结果数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆