Search This Blog

Apr 14, 2011

Using mysql_select_db function

Before we can use the mysql_select_db function, we need to create first a connection to a database.

Connecting to a database is very simple. Here is the sample code.


$connection= mysql_connect("your_host","your_username","your_password");
if (!$con)
  die('Could not connect: ' . mysql_error());

After connecting to the database, what we need to do next is to select the database we will be interacting with. We have to call it using mysql_select_db function.

Lets first look at the basic syntax:

<?php mysql_select_db("database_name", $connection);?>

for example, I have a database named "sampledb" and I had used a variable "$connection" for my mysql connection. The code would look like this:

That’s it, you are now successfully connected and you have selected your database as well.

Happy coding!!!