首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP显示mysql表数据

问用PHP显示mysql表数据
EN

Stack Overflow用户
提问于 2014-07-18 19:02:52
回答 2查看 164关注 0票数 0
代码语言:javascript
复制
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)

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

代码语言:javascript
复制
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)

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-18 19:19:38

当你想要数组的时候,你在使用阿索克

代码语言:javascript
复制
//print the items   
while($row = mysql_fetch_assoc($q)) {
  echo $row['one'] . " " . $row['two'];
  echo "<br>";
}

另外,基于您的php,您的mysql_query()函数可能也会产生一个错误,因为您的查询是一个带有单引号的字符串,而且您的SQL中有单引号,不能转义。

票数 1
EN

Stack Overflow用户

发布于 2014-07-18 23:07:50

我是在这里遗漏了什么吗?还是您真的只想要one,two输出到PHP?

代码语言:javascript
复制
while($row = mysql_fetch_assoc($q)) {
  echo implode("," array($row['one'], $row['two']));
  echo "<br>";
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24832043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档