mysql_create_db() Function in PHP
What is mysql_create_db() function in PHP?
How to create a database in mysql server?
Explanation
The
Mysql_create_db() function is used to create a database.
Syntaxbool mysql_create_db(string database_name [, resource link_identifier])
mysql_create_db() attempts to create a new database on the server associated with the specified link identifier.
Returns TRUE on success or FALSE on failure.
We can create database in mysql server by using mysql_create_db function. If sufficient permission is there for the user then this function will create a new database specified by the user in MySQL Server. Let us try with this simple example for creating a database.
Example
<?php // Attempt to connect to the default database server $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die ("Could not connect"); if (mysql_create_db ("new_db",$link)) { print ("Database created successfully "); } else { print ("Error creating database: <br><br>". mysql_error ()); } //close connection mysql_close($link) ; ?> |
In this script, replace "mysql_host", "mysql_user" and "mysql_password" with your mySQL login and password supplied by the information you used when you installed mySQL. Function mysql_create_db("new_db") will create new database with the name passed as an argument to this function.
The function mysql_create_db() is not supported by PHP 5. We have to use sql command to create a database in PHP 5. Here is the code to create database in PHP 5
Example:
<?php // Attempt to connect to the default database server $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die ("Could not connect"); $query="CREATE DATABASE IF NOT EXISTS new_db"; //Execute query and print status if (mysql_query($query,$link)) { print ("Database created successfully"); } else { print ("Error in creating database: <br><br>". mysql_error ()); } //close connection mysql_close($link) ; ?> |
In this script query is used to create database instead of mysql_create_db function.
See also: mysql_query()