HTML Templates - PHP Scripts and other web resources created by XTTRUST.

(+40) 357392334 xttrust@live.com

How to connect to MySQL Database using PHP

connect to mysql using php

Connect to MySQL using PHP

In this article, i will explain how to connect to MySQL using PHP and i will present an example.

Connecting to MySQL using PHP is very simple, here is a very basic example on how to connect to MySQL using PHP.

<?php

$mysql = mysql_connect(‘localhost’, ‘username’, ‘password’);
if (!$mysql) {
die(‘Server connection error: ‘ . mysql_error());
}
/* You are now connected to MySQL */

// Select a tatabase
$db = mysql_select_db(‘name’, $mysql);
if(!$db) {
die(“Database connection failed: “. mysql_error());
}

/* close connection */
mysql_close($link);

?>

Explanation:
First we create a new variable called `$mysql` and set the value of connection. In `mysql_connection` in place of `localhost` we type our host, it can be for example: “mysql.server.com”, in place of `username` we type our mysql username, example: “root” and for password we type our username password.
Now we check to see if there was a successfully connection, if not throw an error.

After we have successfully connected to MySQL server we have to specify a database, like in our example. Again we create a new variable so later we can check to see if there was a connection to the database, if not, throw an error. “Name” stands for your database name and “$mysql” stands for the connection where it can be reached.

It was this simple to connect to MySQL using PHP. This is an older version of connection to your MySQL database using php.

If you want to see a new and improved version to connect to MySQL using PHP called MySQLi connection, where ‘i’ stands for “Improved” please read this article.

connect to mysql using php

Leave a Reply

Your email address will not be published. Required fields are marked *