Connect to MySQL

By log2 on Dec 20, 2004

This is a basic script that is needed by any PHP scripter if they are using MySQL in any script:

? 

//set the variables as required
$dbhost = "mysite.com"; 
$dbuser = "username"; 
$dbpass = "password"; 
$dbname = "database_name"; 

//do not edit this, it connects the script 
$db = mysql_pconnect($dbhost,$dbuser,$dbpass); 
mysql_select_db("$dbname",$db); 

?> 

Comments

Sign in to comment.
Concordia   -  Jun 27, 2005

Sometimes it\'s a good idea to define the constants, it makes it more secure.

<?

//set the variables as required
define (DBHOST, \'mysite.com\');
define (DBUSER, \'username\');
define (DBPASS, \'password\');
define (DBNAME, \'database_name\');

//do not edit this, it connects the script
mysql_connect (DBHOST, DBUSER, DBPASS);
mysql_select_db (DBNAME) OR die (mysql_error ());

?>

 Respond  
Maxpower5757   -  Jun 05, 2005

you missed some stuff

<?

//set the variables as required
$dbhost = \"mysite.com\";
$dbuser = \"username\";
$dbpass = \"password\";
$dbname = \"database_name\";

//do not edit this, it connects the script
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die(mysql_error());

?>

<\'s are nice in front of a php tag :P
also the or die is for security reasons and is helpfull

 Respond  
QuickStep   -  Dec 21, 2004

if you are connecting to the mysql db on the same server \"localhost\" works fine aswell, oh and this is useless it has been documented so many times

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.