Josué Alba
08-12-2005, 11:15 AM
Hi i have this code in php and I'm getting a selection of the data base error but I'm sure the database is there ans is well seted up.
Here is my code.
Some one could chek if there is something wrong with it?
<?php session_start(); echo'<html>';
//gets variables
$username=$_POST['username'];
$password=$_POST['password'];
// connect to database with username: database_user and password: password
$db = mysql_pconnect('localhost', 'database_name', 'password');
// error if cannot connect
if(!$db){echo 'Error: could not connect to the database'; exit;}
// select database named database_name
$db = mysql_select_db ("database_name");
// error if cannot select
if(!$db){echo '<b>Error: </b>could not select database'; exit;}
// query database and find a user that matches with the same username and password
$authenticate = mysql_query("select * from users where name='$username' and pass='$password'");
// error if cannot query
if(!$authenticate){echo '<b>Error: </b>could not query database'; exit;}
// mysql_num_rows($authenticate) is the number of rows that matched. therefore...
//if there were no matches or more than one match, do not authenticate
if(mysql_num_rows($authenticate)!=1){echo'<b>Error : </b>Invalid username or password';
session_destroy(); exit;}
//if one match was found, authenticate
if(mysql_num_rows($authenticate)==1){echo'<b>Congr adulations!</b> You have logged in successfuly as '.$username.'<br>';
$_SESSION['username']=$username;
$_SESSION['password']=$password;
echo'Display logged in info here</html>';
}
?>
Here is my code.
Some one could chek if there is something wrong with it?
<?php session_start(); echo'<html>';
//gets variables
$username=$_POST['username'];
$password=$_POST['password'];
// connect to database with username: database_user and password: password
$db = mysql_pconnect('localhost', 'database_name', 'password');
// error if cannot connect
if(!$db){echo 'Error: could not connect to the database'; exit;}
// select database named database_name
$db = mysql_select_db ("database_name");
// error if cannot select
if(!$db){echo '<b>Error: </b>could not select database'; exit;}
// query database and find a user that matches with the same username and password
$authenticate = mysql_query("select * from users where name='$username' and pass='$password'");
// error if cannot query
if(!$authenticate){echo '<b>Error: </b>could not query database'; exit;}
// mysql_num_rows($authenticate) is the number of rows that matched. therefore...
//if there were no matches or more than one match, do not authenticate
if(mysql_num_rows($authenticate)!=1){echo'<b>Error : </b>Invalid username or password';
session_destroy(); exit;}
//if one match was found, authenticate
if(mysql_num_rows($authenticate)==1){echo'<b>Congr adulations!</b> You have logged in successfuly as '.$username.'<br>';
$_SESSION['username']=$username;
$_SESSION['password']=$password;
echo'Display logged in info here</html>';
}
?>