Jump to content

Recommended Posts

Alright so i'm working on a simple login script and when I test the script heres the error i get :
[b]
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/totallyc/public_html/login/index.php on line 27[/b]

And my code for line 27 is :
[code] 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";[/code]

Any suggestions?
Link to comment
https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/
Share on other sites

[quote author=Balmung-San link=topic=110150.msg444763#msg444763 date=1159721726]
It would also be good to tell you that session_start() needs to be the first thing in your script.
[/quote]

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
[code]<?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.';
}
}
}
?>[/code]
ok, change this line:
[code=php:0]$query = mysql_query("SELECT * FROM users WHERE username='$user' AND password='$pass'");
[/code]

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

And see what you get.

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

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:
[code=php:0]if (mysql_num_rows($query) != 0) {[/code]
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.