Create MySQL Database in PHP

How to create a MySQL Database in PHP?

Questions by abhishek ankur

Showing Answers 1 - 6 of 6 Answers

Tejanshu

  • Mar 8th, 2010
 

 mysql_query("CREATE DATABASE databasename", connector) is the answer. Where mysql_query() is the PHP function that uses the CREATE DATABASE mysql command.

Here is a small code snippet. I have marked the PhP line to create a database in bold.

<?php

$db_connect = mysql_connect("hostname","user","password");
if (!$db_connect)

  {
  die('Could not connect: ' . mysql_error());
  }

if (mysql_query("CREATE DATABASE my_db",$db_connect))
  {
  echo "Database created successfully";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($db_connect);
?>

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions