Login Problem (mysql_num_rows(): supplied argument is......)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Login Problem (mysql_num_rows(): supplied argument is......)

Post by mmc01ms »

The script spits out the following errors:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/php32/public_html/login2.php on line 13

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/php32/public_html/login2.php on line 14
Login Fail.

Not sure why this is doing this i am new to php so it's not one i've encountred before. Any help would be appriacated. Code is below:

<?php

include "./db.inc";
$register_script ="./register.php";

function login($username, $password){
global $default_dbname;

$link_id = db_connect($default_dbname);
$query = 'select * from customers '
."where username= '$username' "
." and password= '$password'";
$result = mysql_query($query, $db_connect);
if(!mysql_num_rows($result)) return 0;
else{
$query_data = mysql_fetch_row($result);
return $query_data[0];
}
}


function login_form() {
global $PHP_SELF;
?>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<form method="post" action="<? echo $PHP_SELF ?>">
<div align="center"><center>
<p>Login</p>

<table width="731" height="221" border ="1" cellpadding="2">
<!--DWLayoutTable-->
<tr>
<th width="155" align="right" nowrap>Username: </th>
<td width="115" nowrap> <input type="text" name="username" size="15">
</td>
<td width="356"> </td>
</tr>
<tr>
<th width="155" align="right" nowrap>Password: </th>
<td width="115" nowrap> <input type="password" name="password" size="15">
</td>
<td></td>
</tr>
<tr>
<td height="75" colspan="2" align="center" nowrap>
<input type="submit" value="login" name="submit">
<input type="reset" value="reset" name="reset"> </td>
<td><a href="/~php32/index.php">Home</a> | <a href="/~php32/register.php">Register</a> | <a href="/~php32/logout.php">Logout</a> | <a href="/~php32/login.php">Login </a></td>
</tr>
</table>
</center></div>
</form>
</body>
</html>
<?
}

session_start();
if (!isset($username)) {
login_form();
exit;
}
else {

function validation_check($username){

global $default_dbname;

$link_id = db_connect($default_dbname);
$query = 'select validated from customers '
."where username ='$username'";

$result = mysql_query($query);

if ($result != 1) {
echo "This account has not been validated, you need to validate it before you can use it";
}
}

function check_rank($username){

global $default_dbname;

$link_id = db_connect($default_dbname);
$query = 'select rank from customers '
."where username ='$username'";

$result = mysql_query($query);

if($result >=2){
Header ("Location: admin.php");
exit;
}
}
session_register("username", "password");
$username = login($username, $password);
if(!$username) {
session_unregister("username");
session_unregister("password");
echo "Login Fail." ;
exit;
}
else echo "Welcome, $username";
}
?>
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Whats in ./db.inc ?

And please use extensions (.php) at all time, if it's requested directly it now will give back your source. Not something you want with your database connection :)
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

I'm assuming that you didn't give $db_connect a value in db.inc and instead meant to use $link_id.

Code: Select all

<?php
$link_id = db_connect($default_dbname);
$query = 'select * from customers '
."where username= '$username' "
." and password= '$password'";

// Maybe you meant to do this?
$result = mysql_query($query, $link_id);
//$result = mysql_query($query, $db_connect);
?>
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

Thanks for the speedy reply. db.inc is as followed:

<?php
//db_inc book used to help with this code was Beginning PHP4 by WROX Programming. 30/11/04
$dbhost = 'localhost';
$dbusername = 'username';
$dbpassword = 'password';
$default_dbname = 'dbname';

$MYSQL_ERRNO = ' ';
$MYSQL_ERROR = ' ';

//function db_connect takes our global variables and connects to mysql.

function db_connect(){

global $dbhost, $dbusername, $dbpassword, $default_dbname;
global $MYSQL_ERRNO, $MYSQL_ERROR;

$link_id = mysql_connect($dbhost, $dbusername, $dbpassword);
if(!link_id){

$MYSQL_ERRONO = 0;
$MYSQL_ERROR = "Connection Failed to the host $dbhost.";
return 0;
}
else if(empty($dbname) && !mysql_select_db($default_dbname)){
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
return 0;
}

//Function sql_error returns an easier to read error message then the complicated jargon it usually spits out.

function sql_error(){
global $MYSQL_ERRNO, $MYSQL_ERROR;

if(empty($MYSQL_ERROR)) {
$MYSQL_ERRNO = mysql_errno();
$MYSQL_ERROR = mysql_error();
}
return "$MYSQL_ERRNO: $MYSQL_ERROR";
}
?>

if i change the include "db.inc"; to include "db.php" i get an error such as
Parse error: parse error in /home/php32/public_html/db.php on line 42.

thanks for the tip.
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

this makes it easier to read!

Code: Select all

<?php 
//db_inc book used to help with this code was Beginning PHP4 by WROX Programming. 30/11/04 
$dbhost = 'localhost'; 
$dbusername = 'username'; 
$dbpassword = 'password'; 
$default_dbname = 'dbname'; 

$MYSQL_ERRNO = ' '; 
$MYSQL_ERROR = ' '; 

//function db_connect takes our global variables and connects to mysql. 

function db_connect(){ 

global $dbhost, $dbusername, $dbpassword, $default_dbname; 
global $MYSQL_ERRNO, $MYSQL_ERROR; 

$link_id = mysql_connect($dbhost, $dbusername, $dbpassword); 
if(!link_id){ 

$MYSQL_ERRONO = 0; 
$MYSQL_ERROR = "Connection Failed to the host $dbhost."; 
return 0; 
} 
else if(empty($dbname) && !mysql_select_db($default_dbname)){ 
$MYSQL_ERRNO = mysql_errno(); 
$MYSQL_ERROR = mysql_error(); 
return 0; 
} 

//Function sql_error returns an easier to read error message then the complicated jargon it usually spits out. 

function sql_error(){ 
global $MYSQL_ERRNO, $MYSQL_ERROR; 

if(empty($MYSQL_ERROR)) { 
$MYSQL_ERRNO = mysql_errno(); 
$MYSQL_ERROR = mysql_error(); 
} 
return "$MYSQL_ERRNO: $MYSQL_ERROR"; 
} 
?>
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Your $link_id is not returned from the function.

If change:
mysql_query($query, $db_connect);
into:
mysql_query($query);

It sould work, if not. I want to know if the database connection is made or not. Please 'echo $MYSQL_ERROR' for me @ the end of your script.

And if you changed the name of your db.inc you should allso change:
include "./db.inc";
into:
include "./db.php";
Post Reply