-1

I'm trying to setup a system where if you are looking at your own profile you can't friend yourself, and if you're on another persons profile you can. The issue is I get this error.

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\xampp\htdocs\profile.php on line 60

if($user['id'] != $my_id) {
    mysql_connect('localhost', 'root', '');
    mysql_select_db('All Connected');
    $profile_id = $user['id'];
    $check_friend_query = mysql_query("SELECT id FROM friends WHERE (user_one='$my_id' AND user_two='$profile_id') OR (user_one='$profile_id' AND user_two='$my_id')");
    if(mysql_num_rows($check_friend_query) == 1){
        echo "<a href='#' class='box'>You Are Already Friends!</a>";
    } else {
        echo "<a href='#' class='box'>Send Friend Request!</a>";
    }
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
  • 4
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). The error is pretty self explanatory, the query did not work. You need to add some error checking. – Jay Blanchard May 01 '15 at 12:55
  • Also please stop using capitals on every word, it makes your text hard to read. – Rian Schmits May 01 '15 at 13:04
  • Also see http://www.phptherightway.com/#mysql_extension – contrebis May 01 '15 at 13:11
  • That error message makes no sense as is with this code. Double check that. Most likely you should look at http://stackoverflow.com/a/14067623/476 – deceze May 01 '15 at 13:16

1 Answers1

-2
$con = mysql_connect('localhost', 'root', '');
mysql_select_db('All Connected',$con);

if($user['id'] != $my_id){

       $profile_id = $user['id'];

echo "SELECT id FROM friends WHERE user_one='$my_id' AND user_two='$profile_id') OR (user_one='$profile_id' AND user_two='$my_id')";

$check_friend_query = mysql_query("SELECT id FROM friends WHERE user_one='$my_id' AND user_two='$profile_id') OR (user_one='$profile_id' AND user_two='$my_id')");

if(mysql_num_rows($check_friend_query)>0) {

echo "<a href='#' class='box'>You Are Already Friends!</a>";

 } else {

 echo "<a href='#' class='box'>Send Friend Request!</a>";
               }
Madhu
  • 1
  • 1
  • 1
    Explain what you have changed and why. – Jens May 01 '15 at 13:07
  • Did u got the error again after use this.................. – Madhu May 01 '15 at 13:11
  • Still Get Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\profile.php on line 62 – Levi Johnson May 01 '15 at 13:11
  • sorry, $my_id this variable value where u declared, your not declared na... – Madhu May 01 '15 at 13:15
  • it is declared, just not here $my_id = $_SESSION['user_id']; – Levi Johnson May 01 '15 at 13:17
  • if($user['id'] != $my_id){ $profile_id = $user['id'];//just echo this line run it in your phpmyadmin sql command line there only you can get the correct way to execute echo "SELECT id FROM friends WHERE user_one='$my_id' AND user_two='$profile_id') OR (user_one='$profile_id' AND user_two='$my_id')"; – Madhu May 01 '15 at 13:25
  • i'm not sure what you mean – Levi Johnson May 02 '15 at 06:34
  • echo "SELECT id FROM friends WHERE user_one='$my_id' AND user_two='$profile_id') OR (user_one='$profile_id' AND user_two='$my_id')"; $check_friend_query = mysql_query("SELECT id FROM friends WHERE user_one='$my_id' AND user_two='$profile_id') OR (user_one='$profile_id' AND user_two='$my_id')"); – Madhu May 02 '15 at 06:41
  • http://stackoverflow.com/posts/29987331/revisions – Madhu May 02 '15 at 06:43