Page 1 of 1

Database is not connect

Posted: Wed Jul 16, 2014 2:19 am
by saleem Ghouse
Hello,

Im unable to connect database please anybody help me

<?php
$host = "localhost"; // db host
$user = "username"; // db username
$pass = "password"; // db password
$db = "dbname"; // db name

$conn = mysql_connect("$host", "$user", "$pass", "$db") ;
mysql_select_db($db);

?>

i saved this on config.php and called this file in another file in below format
<?php
include("config.php");
?>

Please any body help me.

Regards,
Saleem

Re: Database is not connect

Posted: Wed Jul 16, 2014 4:21 am
by pbs
First, I would suggest you to use mysqli_ functions instead of mysql_, as mysql_ functions are deprecated in latest PHP version.

Regarding your issue:
You have used database name as fourth parameter to mysql_connect() function which is not valid
check the manual for the same: http://php.net/manual/en/function.mysql-connect.php

Use mysql_error() to check the database connection errors or use this connection code

Code: Select all

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
?>
may this will help you

Re: Database is not connect

Posted: Fri Jul 18, 2014 9:05 am
by rushikant123
<?php

// for hosting server-->
// $con=mysqli_connect("localhost","studiom_rushi","rushi123","studiom_Messages");

// for localhost-->

$con=mysqli_connect("localhost","root","","Details");
if (mysqli_connect_errno())
{
echo "MySql Connection failed:" . mysqli_connect_error();
}

?>