简体   繁体   English

MySQL / PHP:从数据库的1列中获取多个值

[英]MySQL / PHP: Fetch multiple values from 1 column in Database

I'm trying to select multiple values from 1 column in my MySQL database. 我正在尝试从MySQL数据库的1列中选择多个值。 I have a Table 'products' with a column 'category'. 我有一个表“ products”和一列“ category”。 Categories: Home, Garden, Cars, Bicycle etc. I want to fetch the number of products with these categories for statistics. 类别:家庭,花园,汽车,自行车等。我想获取具有这些类别的产品数量以进行统计。 It sounds simple but I can only get it done with allot of code. 听起来很简单,但是我只能通过分配代码来完成它。 I want all of these categories to be variables so I only have to put my variables in my statistics engine to do the calculation. 我希望所有这些类别都是变量,因此只需要将变量放入统计引擎即可进行计算。 Right now this is my code to fetch the number of products with category 'Garden': 现在,这是我的代码,用于获取“花园”类别的产品数量:

$query = "SELECT * FROM products WHERE category='Garden'";
$result= mysql_query($query);
$row   = mysql_fetch_array($result);

echo "$row[category]";

Repeating this for every category does 't seem right to me.. Does anyone understand my question and have a solution? 对每个类别重复此操作似乎对我来说不合适。有人能理解我的问题并找到解决方案吗?

我想这就是你想要的

$query = "SELECT `category`, COUNT(`category`) FROM `products` GROUP BY `category`;";

try this 尝试这个

   $query = "SELECT *, count(*) as counts FROM products group by category";
   $result= mysql_query($query);
   while($row   = mysql_fetch_array($result))
   {
   echo $row['category']." ". $row['counts'];

   }

you have to use while to fetch multiple categories. 您必须使用while来获取多个类别。

you need to group by category to get distinct categories. 您需要按类别分组以获得不同的类别。

Just use a while as a loop to get the data. 只需将一段时间用作循环即可获取数据。

What is While Loop? 什么是While循环?

The do while construct consists of a process symbol and a condition. do while构造由一个过程符号和一个条件组成。 First, the code within the block is executed, and then the condition is evaluated. 首先,执行块中的代码,然后评估条件。 If the condition is true the code within the block is executed again. 如果条件为真,则再次执行块中的代码。 This repeats until the condition becomes false. 重复此过程,直到条件变为假。 Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. 因为do while循环在执行块后检查条件,所以控制结构通常也称为后测试循环。 Contrast with the while loop, which tests the condition before the code within the block is executed.The do-while loop is an exit-condition loop. 与while循环相反,while循环在执行该块中的代码之前测试条件。do-while循环是退出条件循环。 This means that the code must always be executed first and then the expression or test condition is evaluated. 这意味着必须始终先执行代码,然后再评估表达式或测试条件。 If it is true, the code executes the body of the loop again. 如果为true,则代码将再次执行循环的主体。 This process is repeated as long as the expression evaluates to true. 只要表达式的计算结果为true,就会重复此过程。 If the expression is false, the loop terminates and control transfers to the statement following the do-while loop. 如果表达式为假,则循环终止,并且控制权移至do-while循环之后的语句。

Just use the code below to fetch multiple values. 只需使用下面的代码即可获取多个值。

$query = "SELECT * FROM products WHERE category='Garden'";
$result= mysql_query($query);
while($row=mysql_fetch_array($result))
{
echo "$row[category]<br>";
}

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

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