Cant connect to database
Page 1 of 1
Cant connect to database
I am running the below code. Its connects to the server but not the database.Please can anyone spot the issue
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$database = "salts408_snowboards";
if ($username&&$password)
{
$connect = mysql_connect("localhost","salts408_admin","xxxxxxxx") or die ("Could not connect to database");
mysql_select_db($database,$connect) or die ("Could not find database");
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if($numrows !=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if ($username==$dbusername&&$password==$dbpassword)
{
echo "Login successful";
$_SESSION['username']=$dbusername;
}
else
die ("Incorrect password");
}
else
echo "The username you entered does not exist";
}
else
die ("Please enter a username and password");
?>
Re: Cant connect to database
Try This code- Code: Select all
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$database = "salts408_snowboards";
//i think thia ia incorrect logic it should be like this
if (isset($_POST['username']) && isset($_POST['password']))
{
$connect = mysql_connect("localhost","salts408_admin","xxxxxxxx") or die ("Could not connect to database");
mysql_select_db($database,$connect) or die ("Could not find database");
$query = mysql_query("SELECT * FROM users WHERE username='$username'",$connect);
$numrows = mysql_num_rows($query);
if($numrows !=0)
{
$row = mysql_fetch_assoc($query);
$dbusername = $row['username'];
$dbpassword = $row['password'];
echo "Login successful";
$_SESSION['username']=$dbusername;
}
else {
die ("Incorrect password");
}
}
else
{
echo "Please Enter UserName";
}
?>
Re: Cant connect to database
No, that gets the same result. It doesn't get to that part and the exceptions/error trapping works fine, it is not connecting to the Snowboards database and I can;t understand why.Re: Cant connect to database
try changing the name of databaseor learning the database selection query
try to run this simple query and please tell me what's the result
- Code: Select all
<?php
$database = "salts408_snowboards";
$connect = mysql_connect("localhost","salts408_admin","xxxxxxxx");
if($connect) {
echo "Connected To Server";
}
else {
echo "Not Connected";
}
$select = mysql_select_db($database,$connect);
if($select) {
echo "Database Selected ";
}
else {
echo "Database Not Selected";
}
?>
Re: Cant connect to database
I got there in the end, was something different, but thanks for the help.Re: Cant connect to database
could you please like to share what was the problem ?Page 1 of 1