编辑的答案
这是我创建的示例表。(我删除了外键)
CREATE TABLE `products` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(6) unsigned NOT NULL,
`name` varchar(30) COLLATE utf8_turkish_ci NOT NULL,
`unit_id` int(6) unsigned NOT NULL,
`brand_id` int(6) unsigned NOT NULL,
`orignalCost` int(30) NOT NULL,
`saleprice` int(30) NOT NULL,
`deleted` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
)
这是示例插入语句
INSERT INTO `products`VALUES (NULL, '1', 'name1', '1', '1', '100', '200', '0');
INSERT INTO `products`VALUES (NULL, '2', 'name2', '2', '2', '100', '200', '0');
INSERT INTO `products`VALUES (NULL, '3', 'name3', '3', '3', '100', '200', '0');
INSERT INTO `products`VALUES (NULL, '4', 'name4', '4', '4', '100', '200', '0');
INSERT INTO `products`VALUES (NULL, '5', 'name5', '5', '5', '100', '200', '0');
INSERT INTO `products`VALUES (NULL, '6', 'name6', '6', '6', '100', '200', '0');
这是我输入的示例数据。
+----+-------------+-------+---------+----------+-------------+-----------+---------+
| id | category_id | name | unit_id | brand_id | orignalCost | saleprice | deleted |
+----+-------------+-------+---------+----------+-------------+-----------+---------+
| 1 | 1 | name1 | 1 | 1 | 100 | 200 | 0 |
+----+-------------+-------+---------+----------+-------------+-----------+---------+
| 2 | 2 | name2 | 2 | 2 | 100 | 200 | 0 |
+----+-------------+-------+---------+----------+-------------+-----------+---------+
| 3 | 3 | name3 | 3 | 3 | 100 | 200 | 0 |
+----+-------------+-------+---------+----------+-------------+-----------+---------+
| 4 | 4 | name4 | 4 | 4 | 100 | 200 | 0 |
+----+-------------+-------+---------+----------+-------------+-----------+---------+
| 5 | 5 | name5 | 5 | 5 | 100 | 200 | 0 |
+----+-------------+-------+---------+----------+-------------+-----------+---------+
| 6 | 6 | name6 | 6 | 6 | 100 | 200 | 0 |
+----+-------------+-------+---------+----------+-------------+-----------+---------+
这是 db connect 通过 PHP 获取所有数据。
$conn = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
$query = $conn->query("SELECT * FROM `products`;");
var_dump($query);
这是上面的结果。你可以看到我有 6 行。
mysqli_result Object
(
[current_field] => 0
[field_count] => 8
[lengths] =>
[num_rows] => 6
[type] => 0
)
为了循环通过 php 对象使用 foreach
foreach ($query as $key => $value) {
var_dump($value);
}
Foreach 结果就像
Array
(
[id] => 1
[category_id] => 1
[name] => name1
[unit_id] => 1
[brand_id] => 1
[orignalCost] => 100
[saleprice] => 200
[deleted] => 0
)
Array
(
[id] => 2
[category_id] => 2
[name] => name2
[unit_id] => 2
[brand_id] => 2
[orignalCost] => 100
[saleprice] => 200
[deleted] => 0
)
It continues like that...
为了得到只saleprice
使用它,如下所示。
foreach ($query as $key => $value) {
var_dump($value['saleprice']);
}
所以这是你可以得到的。其余的由你决定,无论你想用它做什么。