Jump to content


Photo

MySQL Error - help please


  • Please log in to reply
10 replies to this topic

#1 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 01 October 2006 - 04:53 PM

Alright so i'm working on a simple login script and when I test the script heres the error i get :

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/totallyc/public_html/login/index.php on line 27


And my code for line 27 is :
if (mysql_num_rows($result) != 0) { /* This line is line 27 */
		 	//start the session and register a variable
			session_start();
			session_register('username');
		
			//successful login code will go here...
			$error = "Success";

Any suggestions?

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 01 October 2006 - 04:54 PM

We would need to see the part where $result is set/defined.

#3 Balmung-San

Balmung-San
  • Members
  • PipPipPip
  • Advanced Member
  • 327 posts

Posted 01 October 2006 - 04:55 PM

Yes, post your query. It would also be good to tell you that session_start() needs to be the first thing in your script.
"Paranoia is very useful in this work. ...If your cryptographic system can survive the paranoia model, it has at least a fighting chance of surviving in the real world." - Niels Ferguson & Bruce Schneier

They say there's more then one way to skin a cat.
In programming, there's no wrong way to skin a cat, just more efficent ways of doing so.

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 01 October 2006 - 05:03 PM

It would also be good to tell you that session_start() needs to be the first thing in your script.


That's not entirely true, but it has to be specified before anything is output to the browser.  I think it's good practice to put it at the top of the script though and you're more likely to remember it that way.

As for the problem, you'll probably find that mysql_query() returned false rather than true, so as previously stated, your SQL is probably not quite right.  What's the statement you're using?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 01 October 2006 - 05:05 PM

<?php
//check that the user is calling the page from the login form and not accessing it directly
//and redirect back to the login form if necessary

//check that the form fields are not empty, and redirect back to the login page if they are
if (isset($_POST['Login'])) {
	if (empty($_POST['username']) || empty($_POST['password'])) {
		$result = "Username or Password field appear to be empty.";
	} else {
		// Convert Posted Data to Variables
		$user = $_POST['username'];
		$pass = $_POST['password'];
		
		$dbHost = "localhost";
		$dbUser = "**";
		$dbPass = "**";
		$dbDatabase = "****";
		
		// Connect to Database
		$conn = mysql_connect($dbHost,$dbUser,$dbPass) or die ("Unable to connect to Database");
		mysql_select_db($dbDatabase);
		$query = mysql_query("SELECT * FROM users WHERE username='$user' AND password='$pass'");
		$result = mysql_fetch_array($query);
	
		//check that at least one row was returned
	
		if (mysql_num_rows($result) != 0) {
		 	//start the session and register a variable
			session_start();
			session_register('username');
		
			//successful login code will go here...
			$error = "Success";
	
			//we will redirect the user to another page where we will make sure they're logged in
			//header( "Location: checkLogin.php" );
		} else {
			//if nothing is returned by the query, unsuccessful login code goes here...
			$error = 'Incorrect login name or password. Please try again.';
		}
	}
}
?>


#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 01 October 2006 - 05:09 PM

ok, change this line:
$query = mysql_query("SELECT * FROM users WHERE username='$user' AND password='$pass'");

To this:
$query = mysql_query("SELECT * FROM users WHERE username='$user' AND password='$pass'") or die ("Can't execute: " . mysql_error());

And see what you get.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 October 2006 - 05:12 PM

As you ar using a MySQL keyword for a field name  in your users table. You'll want to use the following as the query:
SELECT * FROM users WHERE `username`='$user' AND `password`='$pass'


#8 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 01 October 2006 - 05:12 PM

same error :
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/totallyc/public_html/login/index.php on line 27

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 October 2006 - 05:16 PM

Lookimg at your code. You want to use $query rather than $result on this line:
if (mysql_num_rows($result) != 0) {

mysql_num_rows requires the result resource from mysql_query. Not from mysql_fetch_array. mysql_fetch_array returns the results from the query into an array.

OS the above line should be this:
if (mysql_num_rows($query) != 0) {


#10 alexcrosson

alexcrosson
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 01 October 2006 - 05:18 PM

wow that did it . thanks wildteen and huggie and all others that helped  ;D

#11 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 01 October 2006 - 05:18 PM

I didn't even notice that :(

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users