简体   繁体   中英

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.

$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.

$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.

Any help is greatly appreciated, thank you. 表

remove uid from where..

just apply groupBy on uid, i don't know the syntax but in simple sql statement it would be like

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. To obtain the result of the sql statement you need to do the follow

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. Or just try to var_dump($ticketsrow) to get clue how to access count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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