Saturday, June 16, 2007

PHP and mysql databse episode I

Hi again, sorry for the delay for blogging code especially in this very lovely language. PHP..

So every beginner in php want to know how he can connect to a mysql server and retrieve data from the database and present it in his webpage.

In this lesson I will present the first portion of this ..

HOW to connect to the database.

Before you copy the code.. you have to know that the code below needs to have some changes before that can work correctly..

So you have to change the values of the variables (connection parameters: servername, databasename, user, password)..

After running the script you will got a message saying that the connection has been attempted..

If you got anything else, then you have an error somewhere. If you need my help, comment out here. And I will help soon..


//this code is presented by Developpers Blog.. Http://developpers.blogspot.com

//it's this very simple way to connect to a database

//all what you need is to change the variables values with correct values.

//list of variables, you may need to change the values between quotations

$host = "localhost";

$database = "mydatabase";

$user = "user";

$pass = "password";

//try to keep this lines without changes, only if you know what you are doing.

//you can change the text between quotation if you want

$conn = mysql_connect($host,$user,$pass) or
die(mysql_error());

if ($conn){

$selectdb = mysql_select_db($databse);

if($selectdb){

echo "Connection attempted";
}

else{

echo"Can't connect to the databse. please contact the system administrator.";

}

}

else{

echo "Can't connect to the server..please contact the system administrator.";

}

?>





you can download the .php file from these links below:

Download link 1
Download link 2





No comments: