...

View Full Version : connecting to database



turpentyne
07-25-2012, 08:44 PM
Maybe somebody can see what I'm doing wrong? my database connection is kicking out the 'error selecting' alert I put in. But I'm not sure where the problem is, as everything looks correct for database name



Here's the code:

<?php



function dbconn() {



if(!include_once('../filepath/dbc_include.php')) {
die('Error include file...');
}


if(!$link = mysql_connect($db['hostname'],$db['username'],$db['password'],$db['database'])) {
die('Error connecting...');
}


if(!mysql_select_db($db)) {
die('Error selecting...');
}

return $link;


echo @mysql_ping() ? 'true' : 'false';

}
$link = dbconn();

?>




and here's the include
<?php

$db = array(
'hostname' => 'xxx.xxx',
'username' => 'redacted1',
'password' => 'redacted2',
'database' => 'redacted1',
);


?>

Fou-Lu
07-25-2012, 08:49 PM
$db is an array. It cannot be used as the database name, that should be $db['database'].

MySQL_Connect also does not accept a parameter for the database to select. The fourth parameter is to dictate if it should establish a new connection with the corresponding host, username and password, or if it should reuse an existing one that has connected with the same parameters. MySQLi on the otherhand does require that the database is provided (and you should consider updating to mysqli or pdo anyway).

turpentyne
07-25-2012, 09:54 PM
ah... that fixed it..

but now, I have to confess... mySQLi? This is something new to me - not that I'm all that experienced anyway, but I'll have to check that out...

Do I need to do anything different with the database itself, or is that just new/extended functionality in the commands?

Keleth
07-25-2012, 10:39 PM
mysqli and PDO are object based database connections, complete with data sanitation built in (if you do it correctly).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum