Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create database in MySQL using PHP?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 187
    Comment on it

    In PHP, to create database in MySQL functions mysql_create_db() or mysql_query() is used. To create database using mysql mysql_create_db() syntax is as follows:

    mysql_create_db($dbname); 
    

    For example: Suppose we want to create a database demo.

    Create Database
    
    <?php 
        $link = mysql_pconnect("localhost", "mysql_username", "mysql_password") 
            or die("Could not connect: " . mysql_error()); 
    
        if (mysql_create_db("demo")) { 
            print ("Database created successfully\n"); 
        } else { 
            printf ("Error creating database: %s\n", mysql_error()); 
        } 
    ?>
    

    In above program first you connect to the server then you will create database using mysql_create_db(). If database created successfully it returns true else returns false.

    To create database using mysql_query() function syntax is as follows:

    mysql_query("CREATE DATABASE demo");
    

    For example:

    <?php
    
    // hostname or ip of server
    $servername='localhost';
    
    // username and password to log onto db server
    $dbusername='username';
    $dbpassword='password';
    
    $link=mysql_connect ("$servername","$dbusername","$dbpassword")
    or die ( " Not able to connect to server ");
    
    $query="CREATE DATABASE demo";
    if (mysql_query("$query")) {
    print ("Database created successfully <br>");
    } else {
    print ("Error in creating database:". mysql_error ());
    }
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: