<?php
Create a database
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}
if (mysql_query ("CREATE DATABASE my_db", $con))
{bbs.it-home.org
echo "Database created";
}
Else
{
echo "Error Creating Database:". Mysql_error ();
}
Mysql_close ($con);
?>
2, CREATE table to create tables for database tables in MySQL. Syntax Create TABLE table_name (column_name1 data_type,column_name2 data_type,column_name3 data_type,.......) In order to execute this command, I must add the CREATE TABLE statement to the mysql_query () function. example, create a table named "Persons", which has three columns. The column names are "FirstName", "LastName", and "age":
<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}
Create Database
if (mysql_query ("CREATE DATABASE my_db", $con))
{bbs.it-home.org
echo "Database created";
}
Else
{
echo "Error Creating Database:". Mysql_error ();
}
Create table in my_db database
mysql_select_db ("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar (15),
LastName varchar (15),
Age int
)";
mysql_query ($sql, $con);
Mysql_close ($con);
?>
Copy Code Important: Before you create a table, you must first select the database. Select the database by using the mysql_select_db () function. Note: When you create a database field with a varchar type, you must specify the maximum length of the field, for example: varchar (15). MySQL data type various MySQL data types:
<?php
$sql = "CREATE TABLE Persons
(
PersonID int not NULL auto_increment,
PRIMARY KEY (PersonID),
FirstName varchar (15),
LastName varchar (15),
Age int
)";
mysql_query ($sql, $con);
|