Friday, March 09, 2012

Creating Database with php and mysql

<?php

//the example of making MySQL database
//create.php
$con = mysql_connect("localhost","root","");

if($con){
    echo("Connection is succeed");
}else{
    echo("Connection is fail");
}

$sql = 'CREATE DATABASE my_db';

if (mysql_query($sql, $con)) {
    echo "Database my_db created successfully\n";
} else {
    echo 'Error creating database: ' . mysql_error() . "\n";
}

?>


Another code for it:

<?php
//the example of making MySQL database
//create.php
$con = mysql_connect("localhost","root","");
if($con){
    echo("Connection is succeed");
}else{
    echo("Connection is fail");
}

if (mysql_query("CREATE DATABASE test_db",$con)) {
    echo '<br><br>Database test_db created successfully';
}

else {
    echo '<br><br>Error creating database: ' . mysql_error() . "\n";
}
?>

No comments: