Ryan Smith Ryan Smith - 4 months ago 10
SQL Question

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

Answer

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