Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In php I am doing connection to the mysql database in a simple way like this

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

But I want a more professional way of database connection in an array(where all the variable will be in an array) where I can use the variable like MySQL port number within that. So can someone kindly tell me how to do this? Any help and suggestions will be really appreciable. Thanks..

share|improve this question
    
what exactly do you want to achieve? why array? –  user1646111 Jul 31 '13 at 7:32
    
I want array so that a person who don't have any idea about coding can easily change all the required values... –  NewUser Jul 31 '13 at 7:33
    
what is the purpose? –  gabriel Jul 31 '13 at 7:33
    
Don't use mysql_* extension as they’re deprecated. Use PDO or MSQLi instead. –  X.L.Ant Jul 31 '13 at 7:34
    
Have you ever heard of CONSTANTS? –  karlingen Jul 31 '13 at 7:35
add comment

4 Answers

For more professional, start by worrying about switching to PDO. There are tons of tutorials on it. Same SQL, just different way of working with it.

Beyond that it doesn't really matter if it's an array or separate variable if you're using globals like this but procedural programming is fine when needed.

My main advice is whenever you need to use the db, use a single configuration file you include to gather these variables, b/c too many times when i clean up sites like this later i have to go hunting through 50 different files to update connection strings.

$dbconfig = array(
    'user' => 'dbusername',
    'pass' => 'dbpassword',
    'host' => 'dbhostaddr',
    'name' => 'dbhostname',
);
$link = mysql_connect($dbconfig['host'], $dbconfig['user'], $dbconfig['pass']);


// later you'd use
mysql_select($dbconfig['name']);

edit: sigh i always forget my $'s

share|improve this answer
add comment

You can give the array values like below.

$config = array ('hostname'   => 'localhost',
            'database'   => 'DB_NAME',
            'username'   => 'MYSQL_USER',
            'password'   => 'MYSQL_PASSWORD');

$link = mysqli_connect($config['hostname'],$config['username'],$config['password'],$config['database']);
share|improve this answer
add comment

The way i make connection is `

define ('DB_HOST', 'xxxxx') ;
define ('DB_USER', 'xxxxx') ;
define ('DB_PASS', 'xxxxx') ;
define ('DB_NAME', 'xxxxx') ;

//create connection
$con = mysqli_connect ( DB_HOST, DB_USER, DB_PASS, DB_NAME) ;

//check connection
if (mysqli_connect_errno ( $con))
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error() ;
    }

`

I hope this was any helpful?

share|improve this answer
    
I would suggest adding how to define a constant, based on lack of knowledge of array syntax above, i do not think he would be familiar with that. –  Moylin Jul 31 '13 at 7:40
add comment

You can use mysqli, it will store results in array like in example:

$conn = new mysqli("localhost", "user", "password", "database");
$getName = mysqli_fetch_array($conn, "select name from tblNames where name = 'John'");

Then you would get values in array

echo $getName[0];
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.