Prior to this see How to connect to host in PHP
Here this is a procedure by which you can create and select your database. But creating a database with localhost is possible, it isn’t possible on other hosting.
How to create a database. Open your localhost via browser (http://localhost/). Now goto phpmyadmin (http://localhost/phpmyadmin). Now you will see the option of create a Database. Enter the name and click on create. For newer versions of PHPmyadmin, there will be a tab named databases. Click on that, enter the specified name of your database and click on create. (Here my database name is Example. Everything is case-sensitive).
Now you can connect to the database using your PHP code. The code for how to select database is here..
<?php
//connecting to host
mysql_connect("localhost", "root", "");
//Selecting a specific Database from available databases
if(!mysql_select_db("Example"))
{
echo 'Error'.mysql_error();
}
else
{
echo 'Database selected';
}
?>
Explanation: mysql_select_db() is used to select one of the Database from available databases. You need to provide the name of the database as an argument to mysql_select_db(). It return a non zero value on success and zero with an error, which will be displayed by using mysql_error() inside ‘ if ‘ statement.






7 Comments on "Creating and selecting a database in PHP"
Very interesting subject, thanks for putting up.
Wonderful learn, I simply handed this onto a colleague who was doing some research on that. And he truly bought me lunch as a result of I discovered it for him smile So let me rephrase that: Thanks for lunch! Anyway, in my language, there arent much good supply like this.
An engrossing communication is worth note. I guess that you should make many on this topic, it power not be a bias topic but generally people are not enough to utter on much topics.
I just want to say I am all new to blogging and really enjoyed your web page. More than likely I’m going to bookmark your blog post . You really come with superb articles and reviews. Thanks a lot for sharing your web site.
Hey, I just hopped over in your world-wide-web webpage by means of StumbleUpon. Not somthing I may possibly in most cases browse, but I liked your views none the considerably less. Thanks for building some point properly value browsing.