Jump to content

MySQL Num Rows Question ...


techiefreak05

Recommended Posts

I think its num rows anyway.. wel i have a "friend" system on my site. and im upgrading it, and im trying to implement a condition that if you try to request someone and you are already friends with them display an error, and if youre not, then show a confirmation sentance with an "add" button ... heres the code i have for the confirm friend request page:

[code]
<?php
session_start();
include("func.php");
include("login.php");
?>

<?php PageTop(); ?>

<?php
if($logged_in){
$sql = "SELECT * FROM users WHERE id = '$_GET[userID]' LIMIT 1";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
$userid=$row['id'];
$dispname=$row['dispname'];
}

function userExists($a2){
  global $conn;
  $q2 = "select id from users where id = '$a2'";
  $result2 = mysql_query($q2,$conn);
  return (mysql_num_rows($result2) > 0);
}

function isFriend($a,$b){
  global $conn;
  $q = "select `friend`,`username` from `friends` where `friend` != '$a' and `username` != '$b'";
  $result = mysql_query($q,$conn);
  return (mysql_num_rows($result) > 0);
}

if($_POST['confirmAdd']){
  if(userExists($_GET['userID'])){
mysql_query("INSERT INTO `friends` ( `username` , `friend`, `accepted`, `requester`)
VALUES ('$_SESSION[id]', '$userid', '', '$_SESSION[id]');") or die("<font color=white>There was an error adding friend.</font>");
mysql_query("INSERT INTO `friends` ( `username` , `friend`, `accepted`, `requester`)
VALUES ('$userid', '$_SESSION[id]', '', '$_SESSION[id]');") or die("<font color=white>There was an error adding friend.</font>");
}
}
if(isFriend($_GET['userID'],$_SESSION[id])){
?>
Are you aure you want to ask <b><?php echo $dispname; ?></b> for friendship?<br><br>
<form action="" method="post">
<input type="submit" value="-Yes, I'm Sure-" name="confirmAdd">
</form>
<br><br>If you do not want to request, just simply navigate away from this page.
<?php
}else{
echo "You are already this person's friend!";
}
}
PageBottom();
?>
[/code]

i dont get any errors, but it says im already friends with everybody, even the ones im not friends with... can anyone help with my problem!?!?!?!?!?!

~brenden
Link to comment
https://forums.phpfreaks.com/topic/26157-mysql-num-rows-question/
Share on other sites

I'm confused by your query in the isFriend function:
[code]$q = "select `friend`,`username` from `friends` where `friend` != '$a' and `username` != '$b'";[/code]

You are apparently looking for rows where the 'friend' is not the user ID of the friend and the 'username' is not the userID of the logged in user. So, if you are checking to see if you have Bob as a friend, that query will return results for ANY friend relationships that are not between you and Bob. For example if you have a relationship with John it will be returned, or if Jack has a relationship with Jill, etc.

Also, the way your code is written you expect isFriend to be false whent he person is their friend and true when they are not. You should either change the function to 'notFriend" or change the logic in that if/else conditional.

You need to make your query like this:
[code]<?php
function isFriend($a,$b){
  global $conn;
  $q = "select `friend`,`username` from `friends` where `friend` = '$a' and `username` = '$b'";
  $result = mysql_query($q,$conn);
  return (mysql_num_rows($result) > 0);
}

?>[/code]

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.