Creating and selecting a database in PHP

avatar December 23, 2011 by: 7

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.

See Next: How to create and insert into a table in a database in PHP

Share this Story
Categories
Php  
Tags
Add a Comment on "Creating and selecting a database in PHP"

    7 Comments on "Creating and selecting a database in PHP"

  1. avatar Junko Nieto says:

    Very interesting subject, thanks for putting up.

  2. avatar Scott Vela says:

    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.

  3. 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.

  4. 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.

  5. 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.

Follow us

About Techdigit

Tech Digit comprises of many authors but the CEO of techdigit is Sriram who is an internet Marketer and Entrepreneur.