0

I'm trying to print out only the number of rows containing user2_ids within my streamdata_feedback table and it seems to be printing out both the user2_id and the user1_id. How can I rectify this issue with the below code?

$user1_id=$_SESSION['id'];
$user2_id=$data['id'];

$likesqltwo = "SELECT *
    FROM streamdata_feedback
    WHERE feedback_rating = 1
        AND feedback_userid = " . $user2_id .
        "AND feedback_streamid = " . $streamid;

$likequerytwo = mysql_query($likesqltwo);
$num3 = mysql_num_rows($likequerytwo);
if ($num3 > 0)
{
    echo "And <a title = 'See who likes " .
        $poster_name['fullusersname'] .
        "s status' href='include/likes.php?streamitem_id=" .
        $streamitem_data['streamitem_id']."' />" . 
        $num3 . " ";
}
JYelton
  • 35,664
  • 27
  • 132
  • 191
dave
  • 1,009
  • 5
  • 15
  • 26

1 Answers1

1

Do you have a client for your MySQL database? I'd recommend picking up SqlYog Community Edition. You can then execute your query against it and see how many rows are returned outside of PHP code. Once you have a satisfactory query, then incorporate it into your PHP project.

Next tip: You can include variables from PHP directly in strings with double quotes. PHP will parse the variable, and you needn't concatenate. For example:

$likesqltwo =
    "SELECT *
    FROM streamdata_feedback
    WHERE feedback_rating = 1
    AND feedback_userid = $user2_id
    AND feedback_streamid = $streamid;";

However this code still has a potential flaw, and that's SQL injection attacks. So ensure that the variables you include are not coming from users, or follow the link to learn more about preventing such things. That's not what you asked for help on, but I thought it was worth a mention.

To find out how many times the feedback_userid equals $user2_id for a particular feedback_streamid and for only feedback_rating values of 1, you could try the following query:

SELECT COUNT(id)
FROM streamdata_feedback
WHERE feedback_rating = 1
AND feedback_userid = 123
AND feedback_streamid = 456;

Substitute your primary key for id, and the correct user id and stream id for 123 and 456 respectively. If you get an unexpected number of results, I recommend removing COUNT(id) and selecting the columns of interest so you can inspect and see why you're getting more rows than you thought.

Community
  • 1
  • 1
JYelton
  • 35,664
  • 27
  • 132
  • 191
  • Thank you for your suggestions. But the last bit doesn't really help me. I could put the ids in manually, but I need the php to do that. All I need is the number of likes on a given post echoed out not including the like I've made that is obviously the currently logged in $_SESSION['id'] in the database In some respect my code needs to say..If user2_id has liked this post echo '1 other person liked this' else echo nothing. Tho I don't know how to write it. Currently it shows YOU and 1 other likes this post. When only I've liked it. Its clearly selecting my like from the database. – dave Aug 12 '12 at 23:00
  • I tell a lie. Its working on the top post. Just not the rest below it.This is weird. – dave Aug 12 '12 at 23:23