Connecting to a MySQL database with PHP


In this tutorial, i will tell, how to connect to an existing MySQL database with an existing username and password.

For the first, we need the database and the credentials for it. If you are using a free web hosting service with PHP and MySQL support, you already have this information.

If you have a web server installed with PHP and MySQL, you have to create a database and a username. Since this is not part of this tutorial i will talk about it in another post.

Now, it’s time to get connected. We will create the db.inc.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
//database connection file// for first, we need to set database name, host, username and password for our connection. Modify them corresponding to your data.

$dbhost
=“localhost”;
$dbusername
=”your_username”;
$dbpassword
=“yourpassword”;
$db
=“your_db”;

//If you filled in your data, it’s time to make the connection. Don’t modify anything below this line

$connector = mysql_connect($dbhost, $dbusername, $password) or die(Could not connect to MySQL!);

$db = mysql_select_db($db, $connector) or die(“Couldn’t select database.”);

Leave a Reply

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

Meet the challenge * Time limit is exhausted. Please reload CAPTCHA.