Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从sql查询中计算列数_Php_Mysql_Sql_Aggregate Functions - Fatal编程技术网

Php 从sql查询中计算列数

Php 从sql查询中计算列数,php,mysql,sql,aggregate-functions,Php,Mysql,Sql,Aggregate Functions,我试图使用count()函数计算从表中选择的行数。但对于选择了一行或多行的每个查询,它总是返回“2”,或者对于没有选择行的每个查询,都返回“1” $sql_usb="select item_name from req_item where item_name='USB Dongle'"; $result_usb=mysql_query($sql_usb); $row_usb=mysql_fetch_array($result_usb); $sql_router="select item

我试图使用count()函数计算从表中选择的行数。但对于选择了一行或多行的每个查询,它总是返回“2”,或者对于没有选择行的每个查询,都返回“1”

 $sql_usb="select item_name from req_item where item_name='USB Dongle'";
 $result_usb=mysql_query($sql_usb);
 $row_usb=mysql_fetch_array($result_usb);

 $sql_router="select item_name from req_item where item_name='Access Point/Router'";
 $result_router=mysql_query($sql_router);
 $row_router=mysql_fetch_array($result_router);

 $sql_laptop="select item_name from req_item where item_name='Laptop'";
 $result_laptop=mysql_query($sql_laptop);
 $row_laptop=mysql_fetch_array($result_laptop);

 $usb_inv=count($row_usb);
 $router_inv=count($row_router);
 $laptop_inv=count($row_laptop);


 $total_inv=$usb_inv+$router_inv+$laptop_inv;
我还尝试添加isset()(即,
$usb\u inv=count(isset($row\u usb));
) 和mysql_num_rows()(即,
$usb_inv=mysql_num_rows($row_usb))

两者的结果都是1。

您应该使用

$usb_inv = mysql_num_rows($result_usb);
选择COUNT(*)其中….

检查一下

如果您只需要总数,那么仅1个sql就足够了

// please add error handing yourself.
$sql = "select count(*) from req_item where item_name
        where item_name in('USB Dongle', 'Access Point/Router', 'Laptop')";
$result = mysql_query($sql);
$row = mysql_fetch_array($result)
$total = $row[0];

您应该在结果集中使用mysql\u num\u行

像这样

$usb_inv=mysql_num_rows($result_usb);
$router_inv=mysql_num_rows($result_router);
$laptop_inv=mysql_num_rows($result_laptop);
这将为您提供正确的输出。

您应该使用

$usb_inv = mysql_num_rows($result_usb);
而不是

$usb_inv=count($row_usb);

mysql_fetch_数组返回类似于

$row[0] = ...;
$row[item_name] = ...;
如果使用数组计数,它将始终返回一个2,
因为它返回一个包含索引和关联键的数组

数组mysql\u fetch\u数组(resource$result[,int$result\u type=mysql\u BOTH])

mysql_fetch_array-将结果行提取为关联数组、数字数组或两者

尽管mysql_fetch_数组使用错误,
您可能应该只使用一个查询

$sql ="select item_name from req_item 
       where item_name in('USB Dongle', 'Access Point/Router', 'Laptop')";
此后,

$count = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  // do something else ...

  // to get count for each item name
  $count[$row["item_name"]]++;
}

在mysql查询中必须使用count,如下所示

$SelectQuery = 'select Count(item_name) as [TotalUSB] from req_item where item_name='USB Dongle'
然后你就可以通过

$result = mysql_query($SelectQuery);
$row = mysql_fetch_array($result);

$USBCount = $row['TotalUSB'];

如果op只需要总计数,那么没有必要这样做。当然,但是谁知道op实际需要什么呢?你甚至可以看到他在做
$row\u laptop=…
是的,OP已经检查了正确的答案,显然没有其他东西:)