[英]MySQL count(*) not returning rows with same input
I honestly have no idea what I'm doing with this one, spent atleast 1 hour looking for people with the same issue but couldn't find anything that would suit me / fix my issue. 老实说,我不知道我正在做什么,花了至少1个小时寻找有同样问题的人,但找不到任何适合我/修复我的问题的人。
$auth_user = new USER();
$user_id = $_SESSION['user_session'];
$asd = $auth_user->runQuery("SELECT COUNT(*) FROM ticket_replies WHERE uid=:userid");
$asd->execute(array(':userid'=>$user_id));
$rows = $asd->fetchAll();
$numrows = count($rows);
echo $numrows;
Not even sure if this is correct, but it does return 1 on the page. 甚至不确定这是否正确,但确实在页面上返回1。
$auth_user = new USER();
$user_id = $_SESSION['user_session'];
$asd = $auth_user->runQuery("SELECT uid, count(*) FROM ticket_replies WHERE uid=:uname");
$asd->execute(array(':uname'=>$user_id));
$ticketsrow = $asd->fetchAll();
$count = count($ticketsrow);
foreach($ticketsrow as $row9){
echo $row9['uid'];
}
The code above returns the value of '5' which is one if the values in the table, but obviously I wish for it to return in the 1, 2 & 3 orderly fashion. 上面的代码返回'5'的值,如果表中的值是1,但显然我希望它以1,2和3的顺序返回。
remove uid from where.. 从哪里删除uid ..
just apply groupBy on uid, i don't know the syntax but in simple sql statement it would be like 只是在uid上应用groupBy,我不知道语法,但在简单的sql语句中就是这样
SELECT *,count(*) AS count FROM `ticket_replies` group By `uid` order by `count`
remove this also 删除它也
$numrows = count($rows);
do this 做这个
foreach($ticketsrow as $row9){
echo $row9['uid'];
echo $row9['count'];
}
and you are good to go... 你很高兴...
Lets fix the issues with your first code block do the following 让我们解决您的第一个代码块的问题执行以下操作
$auth_user = new USER();
$user_id = $_SESSION['user_session'];
$asd = $auth_user->runQuery("SELECT COUNT(*) AS `count` FROM ticket_replies WHERE uid=:userid");
$asd->execute(array(':userid'=>$user_id));
$rows = $asd->fetchAll();
fetchAll
function retuns an array so when you use count($rows)
what you get is the size of the array and not the result of the sql statement. fetchAll
函数返回一个数组,所以当你使用count($rows)
,你得到的是数组的大小而不是sql语句的结果。 To obtain the result of the sql statement you need to do the follow 要获取sql语句的结果,您需要执行以下操作
print_r($rows)
echo $rows[0]['count']; // print the result of the sql
To fix the problem with your second code block do the following 要解决第二个代码块的问题,请执行以下操作
$auth_user = new USER();
$user_id = $_SESSION['user_session'];
$asd = $auth_user->runQuery("SELECT *, COUNT(*) AS `count` FROM ticket_replies GROUP BY `uid` ORDER BY `count`");
$asd->execute();
$ticketsrow = $asd->fetchAll();
print_r($ticketsrow);
foreach($ticketsrow as $row){
echo $row['uid'];
echo $row['count']
}
To get the count from the below query 从以下查询中获取计数
select count(*) as count from FROM ticket_replies WHERE uid=:uname
Modify your code something like this 修改你的代码就像这样
echo $ticketsrow[0]['count'];
The above query will always return exactly one row since you are passing userid in the where clause. 由于您在where子句中传递userid,因此上述查询将始终返回一行。 Or just try to var_dump($ticketsrow) to get clue how to access count.
或者只是尝试var_dump($ ticketsrow)以获得如何访问计数的线索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.