繁体   English   中英

在PHP中显示mysql表数据

[英]Display mysql table data in PHP

INSERT INTO `stock` (`stock_id`, `product_attributes`, `product_id`, `qty`) VALUES
(43, '9,11', 2, 0),
(43, '9,12', 2, 10),
(44, '9,13', 2, 20),
(45, '10,11', 2, 0),
(46, '10,12', 2, 30),
(47, '10,13', 2, 50),
(48, '14,11', 2, 0),
(49, '14,12', 2, 0),
(50, '14,13', 2, 0);

我有一个这样的数据表,产品列数据为子字符串格式,即(9,11)

我必须列出所有出现的数量为零的产品。

SELECT 
    product_attributes,
    substring_index(product_attributes,',',-1) as one,
    substring_index(product_attributes,',',1)as two
FROM 
    (SELECT * 
     FROM stock 
     ORDER BY substring_index(product_attributes,',',1) ASC,
              substring_index(product_attributes,',',-1) ASC) AS ordered
WHERE 
    substring_index(product_attributes,',',1) NOT IN (
    SELECT substring_index(product_attributes,',',1)as one FROM stock WHERE qty!='0')
GROUP BY one'

我在数据库中运行了此查询,并获得了正确的输出!

现在我想显示第一列,(逗号)第二列...即(14,11)

mysql_select_db('fly_stock');

// run query
$q = mysql_query('select product_attributes, substring_index(product_attributes,',',-1)as one,substring_index(product_attributes,',',1)as two from(SELECT * FROM stock order by substring_index(product_attributes,',',1)ASC, substring_index(product_attributes,',',-1)asc)as ordered where substring_index(product_attributes,',',1)not in (select substring_index(product_attributes,',',1)as one from stock where qty!='0')group by one');

//print the items   
while($row = mysql_fetch_array($q)) {
  echo $row['one'] . " " . $row['two'];
  echo "<br>";
}
mysql_close($con);
?>

帮我编辑php代码,以便在PHP页面上显示14,11。

您需要mysql_fetch_assoc时使用mysql_fetch_array

//print the items   
while($row = mysql_fetch_assoc($q)) {
  echo $row['one'] . " " . $row['two'];
  echo "<br>";
}

附带说明一下,基于您的php,您的mysql_query()函数也可能会产生错误,因为查询位于带单引号的字符串中,并且SQL中的单引号没有转义。

我是否在这里丢失了某些东西,或者您真的只想要one,two输出到PHP?

while($row = mysql_fetch_assoc($q)) {
  echo implode("," array($row['one'], $row['two']));
  echo "<br>";
}

暂无
暂无

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

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