Wednesday, March 24, 2010

How To Create a mySQL Database Using PHP

mySQL database is most popular database system used in internet. Today I am going to show you how to create a mySQL database using PHP script.
To create a new database, we’ll be using the following syntax here;
CREATE DATABASE database_name
But, we must use the mysql_query() function to execute the above command. This mysql_query() function is used to send a query to a MySQL connection.
After we have base commands ready, we can arrange it to access and create your new MySQL database.
Before we proceed further, you must keep in mind that you must have your web host supporting at least one MySQL database connection and you have full access to your web host control panel. However we are not going to enter the controlpanel zone, but the PHP script surely will have to.
Now let’s assume the following,
$dbhost=’localhost’;  // is your local host name.
$dbusername=’anup’;  // is your web host username
$dbuserpass=’anup123′;  // is your web host cpanel password
$dbname=’anup_database;  // is your new database name to be created
The following script will create a new database named, anup_database.

$dbhost=’localhost’;  // is your local host name.
$dbusername=’anup’;  // is your web host username
$dbuserpass=’anup123′;  // is your web host cpanel password
$dbname=’anup_database;  // is your new database name to be created
$con = mysql_connect ($dbhost, $dbusername, $dbuserpass);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

if ((“CREATE DATABASE $dbname”,$con))
{
echo “Your new Database created”;
}
else
{
echo “Error creating database: ” . mysql_error();
}

mysql_close($con);
?>
$con variable is set to connect to the database, if it fails to it outputs with Could not connect mysql error.
If it becomes successful to create new database, it outputs with Your new Database created. Else it will result Error creating database with mysql error listed.
Here, w are successfully done to create a new database.
Since we’ve created database we can create tables after then.
Let me not end this tutorial right here, else you might be smothering about what to do next.
Let me teach you how to create a table using PHP again.
To Create a table, we are using the following syntax.
CRATE TABLE table_name
(
column_name1 data_type,

column_name2 data_type,
column_name3 data_type,
….

)
It will create the specified table in the specified mysql database name.
While creating we added the CREATE DATABASE syntax in the mysql_query() function to execute it. In the same way we’ll be adding CREATE TABLE syntax to the mysql_query(function).

For an example, lets say that, we need to create a table named “people”, with three columns. And we need the column names: “FirstName”, “LastName” and “Age”.
The following command creates table with $dbname variable (it varies with variable you provide before the command executes), here in our case, anup_database, as we’ve specified $dbname=’anup_database’; as our data base name.
// Create table
mysql_select_db(“$dbname”, $con);
$sql = “CREATE TABLE people
(
FirstName varchar(15),
LastName varchar(15),
Age int
)”;

// Execute query
mysql_query($sql,$con);

mysql_close($con);
Note:The above script is not complete and your browser will never accept ist as a PHP code until your code begins and ends with
Varchar(15) data type allows you to enter only 15 characters (you can see that I’ve added it after the column FirstName and LastName). And int (in the Age column) refers to an integer.
Upto now what far we’ve reached is, with complex combinations of commands and functions, we will be able to create new database with new table and columns.
You can easily combine the both scripts in to one file, like in the example below:

$dbhost=’localhost’;  // is your local host name.
$dbusername=’anup’;  // is your web host username
$dbuserpass=’anup123′;  // is your web host cpanel password
$dbname=’anup_database;  // is your new database name to be created
$con = mysql_connect ($dbhost, $dbusername, $dbuserpass);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

if ((“CREATE DATABASE $dbname”,$con))
{
echo “Your new Database created”;
}
else
{
echo “Error creating database: ” . mysql_error();
}

mysql_close($con);
// Create table
mysql_select_db(“$dbname”, $con);
$sql = “CREATE TABLE people
(
FirstName varchar(15),
LastName varchar(15),
Age int
)”;

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?>
Hope that was pretty helpful.

No comments:

Post a Comment