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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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