How to create a MySQL database using PHP?
1Answer
The CREATE DATABASE statement is used to create a database in MySQL.
and PHP uses mysql_query function to create or delete a MySQL database. This function takes two parameters.
mysql_query( sql, connection );
<html>
<head>
<title>Database Creation</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = 'CREATE DATABASE answerspoint';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not create database: ' . mysql_error());
}
echo "Database answerspoint created successfully";
mysql_close($conn);
?>
</body>
</html>
- answered 7 years ago
- Community wiki
Your Answer