简体   繁体   中英

how to create mysql database from sql query

I need to create a database from MySQL query. I have used the following, but it is not working.

$sql2 = "CREATE DATABASE DATABAE_NAME";
$query2=mysqli_query($connect_dude,$sql2);

This query is not creating any database. Probably,I have to use database username and password, but I'm not quite sure how to apply or use database username, password and host in this case.

Here is a sample php code about connecting DB Hope this will help.

<?php

//mysqli query for connecting database  
//mysqli_connect('host','username','password')
$connection=mysqli_connect('localhost','root','');
if($connection)
{
    //saving query for creating database in a variable 
    $query="CREATE DATABASE student";

    //creating Database
    $newdb=mysqli_query($connection,$query);

    //testing whether database created or not 

    if($newdb)
    {
        //setting database name to connect for database operation
        $setDbName="student";

        //query for connecting database
        $connectionTesing=mysqli_select_db($connection,$setDbName);

        if($connectionTesing)
        {
            echo "Connected!";
        }
    }
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM