Login | Register | FAQ
Anonymous

Cant connect to database

+ Post a reply

6 posts Page 1 of 1


Cant connect to database

by SarahLouLeeming » Fri Jun 01, 2012 12:11 pm

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");
?>
User avatar

SarahLouLeeming

  • Posts: 14
  • Joined: Fri Jun 01, 2012 12:07 pm

Re: Cant connect to database

by XainPro » Sat Jun 02, 2012 4:42 am

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";
}

?>
User avatar

XainPro

  • Posts: 3562
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Cant connect to database

by SarahLouLeeming » Sun Jun 03, 2012 9:41 pm

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.
User avatar

SarahLouLeeming

  • Posts: 14
  • Joined: Fri Jun 01, 2012 12:07 pm

Re: Cant connect to database

by XainPro » Mon Jun 04, 2012 3:46 am

try changing the name of database
or 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";
}

?>
User avatar

XainPro

  • Posts: 3562
  • Joined: Fri Feb 17, 2012 8:10 pm

Re: Cant connect to database

by SarahLouLeeming » Fri Jun 08, 2012 3:39 pm

I got there in the end, was something different, but thanks for the help.
User avatar

SarahLouLeeming

  • Posts: 14
  • Joined: Fri Jun 01, 2012 12:07 pm

Re: Cant connect to database

by XainPro » Sat Jun 09, 2012 5:47 am

could you please like to share what was the problem ?
User avatar

XainPro

  • Posts: 3562
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1

?>