0

this is my first php code , i am trying to connect to a database with user name = "root" and password = "root"

i have a connection file called dbConnection.php as following :

<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";


//connection to the database
$conn = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysql_select_db($db);
echo "db-connection : connection done";
// Check connection

echo "Connected successfully";
?>

and i call it in a file called what.php :

<?php
echo "Hello";
include "dbConnection.php";
echo "ohhhh";
?>

this returns status code 500 which is internal server error

but i want to know what is the error to fix it how can i get the error message ?

i tried

$conn = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

but it is not returning any thing.

can any one help me please ?

3
  • Is MySql running & login credentials are verified? Commented May 16, 2015 at 7:07
  • make sure your server is running. Commented May 16, 2015 at 7:09
  • Check the username and password by run the mysql using commandline Commented May 16, 2015 at 7:20

4 Answers 4

0

if you are having password on localhost then use password otherwise leave it blank

  $con = mysqli_connect("localhost","root","yourpassword","yourdb");
Sign up to request clarification or add additional context in comments.

Comments

0

You mysqli_connect() method.

<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";


//connection to the database
$conn = mysqli_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysqli_select_db($conn,$db);
echo "db-connection : connection done";
// Check connection

echo "Connected successfully";
?>

Comments

0

First Thing is to Change your connection method to mysqli or my personal favorite PDO. PDO Example:

class db extends pdo{
    //Website Variables

    public $sitedb = '';
    public $siteconfig;
    public $sitesettings = array(
        'host'      => 'localhost',
        'database'  => 'yourdb',
        'username'  => 'youruser',
        'password'  => 'yourpass',
    );

    public function __construct(){

        $this->sitedb = new PDO(
            "mysql:host={$this->sitesettings['host']};" .
            "dbname={$this->sitesettings['database']};" .
            "charset=utf8",
            "{$this->sitesettings['username']}",
            "{$this->sitesettings['password']}"
        );

        $this->sitedb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    }


}

$db = new db();

Then you can extend your PDO class to new classes after including the db.php page. Example Select:

   class yourclass extends db {
           public function SelectUsers() {
                    global $db;
                    $query = <<<SQL
                    SELECT email
                    FROM users
                    WHERE active = :active
   SQL;
           $resource = $db->sitedb->prepare( $query );
           $resource->execute( array (
                  ':active' => 1,
                  ));
            $count = $resource->rowCount();
            foreach($resource as $user){
                    $this->email = $user['email'];
              }
          }
         }

Comments

0
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";


// make the current db
$db_selected = mysqli_select_db ( $conn , $dbname );
if (!$db_selected) {
   die ('Can\'t connect to Database : ' . mysql_error());
}


?>

1 Comment

if you can run in localhost than password="" and username ="root"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.