PHP mysql_create_db function

 The main function of the PHP mysql_create_db function is to create a MySQL database, but mysql_create_db has been deprecated and removed in PHP 5.5.0 and newer versions. It should be replaced by MySQLi or PDO_MySQL extensions. The mysqli_query function can replace the mysql_create_db function. And PDO::query function. This article provides the basic syntax of mysql_create_db function and the application method of replacing this function with SQL CREATE DATABASE syntax.


PHP mysql_create_db function syntax
mysql_create_db ( $database name , $link_identifier )
The first parameter $database_name is the name of the database to be created, a necessary item, and the second parameter $link_identifier is the connection to the database server. If it is not filled in, mysql_create_db will automatically call mysql_connect for the most recent link. If it cannot be called If it succeeds, it will try to establish a connection with mysql_connect without parameters. If it still fails, an E_WARNING level error will be generated. If the database is successfully established, TRUE will be returned, and FALSE will be returned on failure.

PHP mysql_create_db function example
 <?php
$link = mysql_connect('localhost','mysql_user','mysql_password');
if (!$link) {
 die('Unable to connect to the database');
}

$sql ='CREATE DATABASE test_database';

if ( mysql_query($sql, $link)){
 echo "Database test_database created successfully";
}else {
 echo'Database test_database failed to create';
}
?>
As shown in the first paragraph, the mysql_create_db function has been abandoned by PHP , but the need to create a database still exists. This example is a simple way to create a database directly through the sql command syntax. The example first uses mysql_connect to create it. A link to the MySQL database server, and then write a paragraph of sql syntax, where "CREATE DATABASE" is the command to create a database in MySQL , test_database is the name of the database we want to create, and finally execute this sql command through mysql_query Syntax, use the if conditional judgment formula to judge whether the establishment is successful, such an architecture can directly replace the function of the mysql_create_db function.

Post a Comment

0 Comments