FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


selecting database





ncwdavid
Hey, When connecting to mysql using php instead of writing down the code everytime I use another php file called xxx.php and I just write down include'xxx.php' and it connects but I can't selecht a database. This is my xxx.php file at the moment:
Code:

<?php

$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
                     ('Error connecting to mysql');
?>               


If I want to select a certain database what do I have to add to the code? Thank You.
mathiaus
Code:
mysql_select_db($dbdb,$conn) or die ("Could not open database");

Set the var $dbdb to the name of your database.

I personally would make it a function for neatness though;
Code:
<?php
function dbconnect($database) {
   $dbhost = 'localhost';
   $dbuser = 'username';
   $dbpass = 'password';

   $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
   mysql_select_db($database,$conn) or die ("Could not open database");
}
?>

You would use this by including the file and underneath add
Code:
dbconnect('databasename');
hexkid
ncwdavid wrote:
If I want to select a certain database what do I have to add to the code?


Code:

<?php

$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
                     ('Error connecting to mysql');

###############
# Add these lines
$dbdata = 'database_to_use';
if (!mysql_select_db($dbdata)) die('Error selecting database');

?>
Related topics
WTF! A strange database just appeared lol
script backup database
How much database
database problem
Implementing your database access in PHP as a layer.
Navigation Script (javascript) Help Please!
how can i change password in my database?
Database Problem
How to restore database?
Need help with database, can anyone help?
How to set up a wiki
PHP & MySQL auth system
maybe i'm daft...
MySQL - Session error?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.