My php scripts!

php scripts I use.

My Photo
Name:
Location: Bucuresti, Bucuresti, Romania



Tuesday, May 03, 2005

Connect to a MySQL database using PHP

Create a file named for example cfg_db_connect.php with the following code:

function dbopen()
{
global $connect;
global $mydbuser;
global $mydbpassw;
global $mymaindb;

$connect = mysql_connect( "localhost", $mydbuser, $mydbpassw );
if ( ! $connect ) die( "Can't connect to database." );
mysql_select_db( $mymaindb, $connect) or die ( "Error:".mysql_error() );
return $connect;
}

function dbclose($connect)
{
mysql_close($connect);
return true;
}

// please change the values:

$dbuser="MY_USER";
$dbpassw="MY_PASSWORD";
$maindb="MY_DATABASE";


You can use this function in any other file that need mysql connection:

include ("cfg_db_connect.php");
$connect=dbopen();
$query="SELECT something FROM table WHERE condition";
$request=mysql_query($query,$connect);
dbclose($connect);

0 Comments:

Post a Comment

<< Home