Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.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 MySQL中特定行的计数_Php_Mysql - Fatal编程技术网

Php MySQL中特定行的计数

Php MySQL中特定行的计数,php,mysql,Php,Mysql,如果我想计算数据库中的一个特定行(未读),我应该如何继续这个MySQL查询?到目前为止,它统计了整张桌子 $result_notifications = mysql_query("select count(1) FROM bhost_notifications where taker_id='$user_info[u_id]'"); $row_notifications = mysql_fetch_array($result_notifications); $total_notification

如果我想计算数据库中的一个特定行(
未读
),我应该如何继续这个MySQL查询?到目前为止,它统计了整张桌子

$result_notifications = mysql_query("select count(1) FROM bhost_notifications where taker_id='$user_info[u_id]'");
$row_notifications = mysql_fetch_array($result_notifications);
$total_notifications = $row_notifications[0];

您需要为该列设置别名

SELECT COUNT(1) AS count ...

然后调用
$row\u followers[count]
。请注意,
mysql\u
函数已被弃用。了解何时传递变量,并使用or-将帮助您决定传递哪个变量

我怀疑您有一个未规范化的数据库。虽然这在某些情况下更可取,但我怀疑你的情况是否如此。如前所述,您无法确保查询将返回所需的行。SQL不保证行的顺序,除非使用ORDERBY子句


这个问题似乎表明了一些语法问题之外的更多问题

随着时间的推移,我已经用PHP编写了一个很好的函数,它允许我轻松地查找记录,但仍然具有足够的动态性,在我执行的每种类型的查询中都很有用

用法:

if (get("select * from table", $query_array) > 0)
{
 // There is at least one row returned
 $result_array = mysql_fetch_array($query_array);
 .
 .
 .
} else {
 // No rows in the set
}
功能:

function get($sql, &$array)
{
 $array = "";
 $q = mysql_query($sql);
 if (mysql_error())
 {
  $ret = -1;
  print "<div><font style='font-family: arial; font-size: 12px;'><font style='color: red;'>Error:</font> " . mysql_error() . "<br>SQL: #sql</font></div>";
  exit(1);
 } else {
  $ret = mysql_num_rows($q);
  if ($ret > 0)
  {
   $array = $q;
  }
 }
 return $ret;
}
函数get($sql,&$array) { $array=“”; $q=mysql\U查询($sql); if(mysql_error()) { $ret=-1; 打印“错误:.mysql_Error()”
SQL:#SQL); 出口(1); }否则{ $ret=mysql\u num\u行($q); 如果($ret>0) { $array=$q; } } 返回$ret; }
如果查询有错误,这也会给出格式化的错误消息。我一直使用它,因为它将mysql\u查询和mysql\u num\u行压缩到一个命令中。

如果您想要行数等于某个值的行数,那么添加where子句
mysql.*
很难从地球上消失。。。。可悲的事实。我很快就要换成PDO了!谢谢你的通知,帮我解决问题。