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";
}
?>
Login Problem (mysql_num_rows(): supplied argument is......)
Moderator: General Moderators
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
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);
?>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.
<?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.
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";
}
?>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";
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";