Click here to Skip to main content
15,796,984 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I would like to write, the total number of likes, but I don't know how I can make this to retrieve the contents of the table
$user = 'root';
$pass = '';
$PDO = new PDO('mysql:host=localhost;dbname=mike', $user, $pass);
$PDO->exec("set names utf8");
$query = "SELECT SUM(likes) as test FROM news WHERE autor=".$_SESSION['id']."";

$c_query = $PDO->prepare($query;
$results = $c_query->fetch(PDO::FETCH_ASSOC);

        echo "Total number of likes: ".$f_test = $results['test'];
catch(PDOException $e)
echo "Error: " .$e;

What I have tried:

with COUNT() and use multiple query
Updated 30-Mar-17 16:48pm
Member 13036251 30-Mar-17 15:54pm    
Have you tried: SELECT COUNT(column_name) FROM table_name; ?
Mike CJ 31-Mar-17 1:04am    
It's working, but I would like get date for logged user

1 solution

This should do it.
$rCount= $PDO->query($query)->fetchColumn(); 
echo '<br/> Total number of likes: '.$rCount;
Share this answer
Mike CJ 31-Mar-17 1:08am    
I would like get all date for logged user.
Bryian Tan 31-Mar-17 1:13am    
can you explain in detail with an example?
Mike CJ 31-Mar-17 1:35am    
The point is that he wants to draw the total number. So if the user has two posts and two users have given them like this. Then the total number is 4. So I wanted to use $ _SESSION ['id']. Would it be better to send uploads to the user id database?
Bryian Tan 31-Mar-17 1:26am    
If it only list of date

foreach($db->query('SELECT YourDateColumn FROM news WHERE autor='.$_SESSION['id']) as $row) {
		echo $row['YourDateColumn'];
Mike CJ 31-Mar-17 1:53am    
Myabe I can use sum() instead count (). I don't know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900