Jump to content

php mysql query SELECT


ctcp

Recommended Posts

table1 users

| id | login | ref |

| 26757| alex| 0

| 26758 | php1| 26757

| 26759 | php2| 26757

 

table2 liked

|id  | user_id |

| 1 | 26758 |

| 2 | 26758 |

| 3 | 26758 |

| 4 | 26758 |

| 5 | 26759 |

i whant to count user_id from liked where login from users 

 

i don't know if this is right way to count but i trying to run this query can sombady help me what is wrong or give me the another way to count user_id

 

<?php
			$query_liked = "SELECT COUNT(user_id) AS Num FROM liked AND (SELECT ref FROM users WHERE login='$x')"; 
?>

 

error message

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND (SELECT ref FROM users WHERE login='simos')' at line 1

 

Link to comment
https://forums.phpfreaks.com/topic/251567-php-mysql-query-select/
Share on other sites

<?php
			// start count referrals likes

			//$query_liked = "SELECT COUNT(user_id) AS Num FROM liked AND (SELECT ref FROM users WHERE login='$x')"; 
			$query_liked = "SELECT COUNT(user_id) AS Num 
			FROM (SELECT user_id FROM `liked` 
			UNION ALL
			SELECT ref FROM `users` WHERE login='$x') AS `t`"; 
 			$result = mysql_query($query_liked) or die(mysql_error());
			$row_count = mysql_result($result,0,"Num");
			echo $row_count;
			//end count
?>


 

 

i found this but i get as result all rows on table how to get result WHERE table1 ref ID = table2 user_id

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.