Jump to content

mysql_num_rows(): supplied argument is not a valid MySQL result resource


pes96

Recommended Posts

PHP Error Message

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource /home/a4199910/public_html/login2.php on line 28

http://teste-vieirasousa.tk/login2.php

 

 

THE CODE: (the values of database, password, username, server that i use here are an example)

<?php

   $link = mysql_connect("localhost", "username", "password");
   mysql_select_db("database_name", $link);

function login($username, $password) {
   $username = addslashes($username);
   $password = md5($password);
   $query = mysql_query("SELECT * FROM user_accounts WHERE username='$username' AND password='$password'");
   if(mysql_num_rows($query) == 1) {
      $info = mysql_fetch_array($query);
      $userid = $info[userid];
      $sessionid = md5($userid . time());
      $time = time();
      @setcookie ('test_account', $sessionid, $time+3600, '/', '');
      mysql_query("DELETE FROM user_sessions WHERE userid='$userid'");
      mysql_query("INSERT INTO user_sessions (sessionid,userid,timestamp) VALUES('$sessionid','$userid','$time')");
      return $userid;
   } else {
      return 0;
   }
}

function status() {
   $sessionid = $_COOKIE[test_account];
   $oldtime = time() - 3600;
   $query = mysql_query("SELECT * FROM user_sessions WHERE sessionid='$sessionid' AND timestamp>$oldtime");
       if(mysql_num_rows($query) == 1) {
      $info = mysql_fetch_array($query);
      return $info[userid];
   }
   return 0;
}

function logout() {
   $sessionid = $_COOKIE[test_account];
   @setcookie ("test_account",'', time()-99999, '/', '');
   mysql_query("DELETE FROM user_sessions WHERE sessionid='$sessionid'");
}

if($_POST[username] !='' || $_POST[password] != '') {
   $login_status = login($_POST[username], $_POST[password]);
} else if($_GET[logout]) {
   logout();
}
$userid = status();


if($userid > 0) { echo "Welcome to our site, user #$userid (<a href='?logout'>Click here to logout</a>)"; } else {

if($login_status != '' && $login_status == 0) { echo "Invalid username/password combo.<br>"; }
?>

<form action="sample.php" method="POST">
<input type=text name=username>
<input type=password name=password>
<input type=submit value="Log In">
</form>

<?php } ?>

 

 

LINE 28:

if(mysql_num_rows($query) == 1) {

 

Anyone can help me to resolve this error?

Thank you

Link to comment
Share on other sites

The query that is failing, based on the line number being listed is the following - $query = mysql_query("SELECT * FROM user_sessions WHERE sessionid='$sessionid' AND timestamp>$oldtime");

 

You need to have error checking and error reporting logic in your code to get your code to tell you when and why something is failing. Try the following for debugging purposes -

 

$query = mysql_query("SELECT * FROM user_sessions WHERE sessionid='$sessionid' AND timestamp>$oldtime") or die(mysql_error());

Link to comment
Share on other sites

You have to learn escaping single quotes in "php varibles".

Take a look at the example:


$str1 = 'string';
$str2 = '$str1';
printf('%s and %s', $str1, $str2);

 

So you can explain me where is the error in my code?

 

Thanks for all

Link to comment
Share on other sites

I do this, but appears the same error.

 

What is your current code? And is there any chance you are using a cheap/free web host where changes you make to your files don't take affect immediately due to disk caching?

 

Now i have done and appears the message: "No database selected"

Link to comment
Share on other sites

Your mysql_select_db() statement is failing, either because the database name you are putting into it doesn't exist or if it does, your database user has not been assigned permission to access it.

 

Again, all code needs error checking and error reporting logic to get it to tell you when and why it is failing. Use the following in place of your mysql_select_db statement -

 

mysql_select_db("database_name", $link) or die("Select db failed: " . mysql_error());

Link to comment
Share on other sites

Your mysql_select_db() statement is failing, either because the database name you are putting into it doesn't exist or if it does, your database user has not been assigned permission to access it.

 

Again, all code needs error checking and error reporting logic to get it to tell you when and why it is failing. Use the following in place of your mysql_select_db statement -

 

mysql_select_db("database_name", $link) or die("Select db failed: " . mysql_error());

 

Now appears: "Select db failed: Access denied for user 'a4199910_login'@'10.1.1.43' to database 'Login'"

Link to comment
Share on other sites

So you can explain me where is the error in my code?

Thanks for all

 

My apologies, your sql  syntax is correct  :-\

First, I thought that the line 28 is just before the query I gave you above.

Secondly, you can make a simple debugging test to check, what actually values you will send to database.

 


<?php
$username = 'jazzman';
$password = 'password';
$id = 1;
$sql1 = "SELECT * FROM user_accounts WHERE username='".$username."' AND password='".$password."' AND user_id='".$id."'";
$sql2 = 'SELECT * FROM user_accounts WHERE username ='.$username.' AND password='.$password.' AND user_id='.$id; // integer id
$sql3 = "SELECT * FROM user_accounts WHERE username='$username' AND password='$password' AND user_id='$id'"; 
$sql4 = 'SELECT * FROM user_accounts WHERE username=$username AND password=$password'; // invalid query

printf('%s<br />%s<br />%s <br />%s', $sql1, $sql2, $sql3, $sql4);

Link to comment
Share on other sites

So you can explain me where is the error in my code?

Thanks for all

 

My apologies, your sql  syntax is correct  :-\

First, I thought that the line 28 is just before the query I gave you above.

Secondly, you can make a simple debugging test to check, what actually values you will send to database.

 


<?php
$username = 'jazzman';
$password = 'password';
$id = 1;
$sql1 = "SELECT * FROM user_accounts WHERE username='".$username."' AND password='".$password."' AND user_id='".$id."'";
$sql2 = 'SELECT * FROM user_accounts WHERE username ='.$username.' AND password='.$password.' AND user_id='.$id; // integer id
$sql3 = "SELECT * FROM user_accounts WHERE username='$username' AND password='$password' AND user_id='$id'"; 
$sql4 = 'SELECT * FROM user_accounts WHERE username=$username AND password=$password'; // invalid query

printf('%s<br />%s<br />%s <br />%s', $sql1, $sql2, $sql3, $sql4);

 

So i have to put this code where?

Link to comment
Share on other sites

So i have to put this code where?

I think, you need to start learning php from beginning - step by step .

 

But you can only help me correcting this code, (saying what i have to do) after i will learn php, but now i need this code working fine.

 

Thanks for all

Link to comment
Share on other sites

So i have to put this code where?

I think, you need to start learning php from beginning - step by step .

 

But you can only help me correcting this code, (saying what i have to do) after i will learn php, but now i need this code working fine.

 

Thanks for all

 

We are here to guide, not do for you.

Link to comment
Share on other sites

So you can explain me where is the error in my code?

Thanks for all

 

My apologies, your sql  syntax is correct  :-\

First, I thought that the line 28 is just before the query I gave you above.

Secondly, you can make a simple debugging test to check, what actually values you will send to database.

 


<?php
$username = 'jazzman';
$password = 'password';
$id = 1;
$sql1 = "SELECT * FROM user_accounts WHERE username='".$username."' AND password='".$password."' AND user_id='".$id."'";
$sql2 = 'SELECT * FROM user_accounts WHERE username ='.$username.' AND password='.$password.' AND user_id='.$id; // integer id
$sql3 = "SELECT * FROM user_accounts WHERE username='$username' AND password='$password' AND user_id='$id'"; 
$sql4 = 'SELECT * FROM user_accounts WHERE username=$username AND password=$password'; // invalid query

printf('%s<br />%s<br />%s <br />%s', $sql1, $sql2, $sql3, $sql4);

 

So and now?

 

Thanks for your patience

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.