Database is not connect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saleem Ghouse
Forum Newbie
Posts: 1
Joined: Wed Jul 16, 2014 2:04 am

Database is not connect

Post 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
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Database is not connect

Post 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
rushikant123
Forum Newbie
Posts: 3
Joined: Fri Jul 18, 2014 8:40 am

Re: Database is not connect

Post 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();
}

?>
Post Reply