0

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.

12
  • 1
    You are neither verifying whether mysqli_query() succeeds nor retrieving the error message if any. Commented Sep 25, 2014 at 16:46
  • @ÁlvaroG.Vicario I verified that and it's giving me failure msg Commented Sep 25, 2014 at 16:47
  • msg says database not created Commented Sep 25, 2014 at 16:48
  • 2
    Make sure you're connected and that you have permissions to create a database. Check your PHPmyadmin's settings. Commented Sep 25, 2014 at 16:49
  • I'm connected for sure.But i don't know how to check the permission to create database.any clue? Commented Sep 25, 2014 at 16:51

1 Answer 1

1

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!";
        }
    }
}

?>
Sign up to request clarification or add additional context in comments.

1 Comment

I already fixed the problem.But I'm talking your one as the solution.Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.