0% found this document useful (0 votes)
66 views

PHP MySQL Create Database and Tables

This document discusses how to create databases and tables in PHP MySQL. It explains how to use the CREATE DATABASE and CREATE TABLE statements to generate a database and table. It also covers setting a primary key, auto-incrementing fields, and data types.

Uploaded by

cetokbatok
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

PHP MySQL Create Database and Tables

This document discusses how to create databases and tables in PHP MySQL. It explains how to use the CREATE DATABASE and CREATE TABLE statements to generate a database and table. It also covers setting a primary key, auto-incrementing fields, and data types.

Uploaded by

cetokbatok
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP MySQL Create Database and Tables

http://www.w3schools.com/php/php_mysql_create.asp

Pilih Bahasa
HOME HTML CSS JAVASCRIPT JQUERY XML ASP.NET PHP SQL MORE...

Search w3schools.com
REFERENCES | EXAMPLES | FORUM | ABOUT

W3SCHOOLS EXAMS Get Certified in: HTML, CSS, JavaScript jQuery, XML, PHP, ASP

PHP Basic
PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP HOME Intro Install Syntax Variables String Operators If...Else Switch Arrays While Loops For Loops Functions Forms $_GET $_POST

PHP MySQL Create Database and Tables


Previous
A database holds one or multiple tables.

w3schools.com
on Facebook

Next Chapter

Suka

20rb

WEB HOSTING Best Web Hosting eUK Web Hosting

Create a Database
The CREATE DATABASE statement is used to create a database in MySQL.

UK Reseller Hosting Cloud Hosting Top Web Hosting

Syntax
CREATE DATABASE database_name

$3.98 Unlimited Hosting 250+ Hosting Apps WEB BUILDING

PHP Advanced
PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP Date Include File File Upload Cookies Sessions E-mail Secure E-mail Error Exception Filter

To learn more about SQL, please visit our SQL tutorial. To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

XML Editor - Free Trial! FREE Website BUILDER FREE Website Creator W3SCHOOLS EXAMS Get Certified in: HTML, CSS, JavaScript, jQuery, XML, PHP, ASP STATISTICS Browser Statistics Browser OS Browser Display SHARE THIS PAGE

Example
The following example creates a database called "my_db":

PHP Database
MySQL Introduction MySQL Connect MySQL Create MySQL Insert MySQL Select MySQL Where MySQL Order By MySQL Update MySQL Delete PHP ODBC

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?>

PHP XML
XML Expat Parser XML DOM XML SimpleXML

PHP and AJAX


AJAX AJAX AJAX AJAX AJAX AJAX AJAX Intro PHP Database XML Live Search RSS Reader Poll

Create a Table
The CREATE TABLE statement is used to create a table in MySQL.

Syntax
CREATE TABLE ( column_name1 column_name2 column_name3 .... ) table_name data_type, data_type, data_type,

PHP Reference
PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP PHP Array Calendar Date Directory Error Filesystem Filter FTP HTTP Libxml Mail Math Misc MySQL SimpleXML

To learn more about SQL, please visit our SQL tutorial. We must add the CREATE TABLE statement to the mysql_query() function to execute the command.

Example
The following example creates a table named "Persons", with three columns. The column names will be "FirstName", "LastName" and "Age":

<?php $con = mysql_connect("localhost","peter","abc123");

1 of 3

11/12/2012 16:35

PHP MySQL Create Database and Tables

http://www.w3schools.com/php/php_mysql_create.asp

PHP String PHP XML PHP Zip

if (!$con) { die('Could not connect: ' . mysql_error()); } // Create database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } // Create table mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )"; // Execute query mysql_query($sql,$con); mysql_close($con); ?>
Important: A database must be selected before a table can be created. The database is selected with the mysql_select_db() function. Note: When you create a database field of type varchar, you must specify the maximum length of the field, e.g. varchar(15). The data type specifies what type of data the column can hold. For a complete reference of all the data types available in MySQL, go to our complete Data Types reference.

PHP Quiz
PHP Quiz PHP Certificate

Primary Keys and Auto Increment Fields


Each table should have a primary key field. A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record. The following example sets the personID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field.

Example
$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);

Previous

Next Chapter

2 of 3

11/12/2012 16:35

PHP MySQL Create Database and Tables

http://www.w3schools.com/php/php_mysql_create.asp

Top 10 Tutorials
HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial PHP Tutorial ASP.NET Tutorial XML Tutorial

Top 10 References
HTML/HTML5 Reference CSS 1,2,3 Reference CSS 3 Browser Support JavaScript HTML DOM XML DOM PHP Reference jQuery Reference ASP.NET Reference HTML Colors

Examples
HTML Examples CSS Examples XML Examples JavaScript Examples HTML DOM Examples XML DOM Examples AJAX Examples ASP.NET Examples Razor Examples ASP Examples SVG Examples

Quizzes
HTML Quiz XHTML Quiz CSS Quiz JavaScript Quiz jQuery Quiz XML Quiz ASP Quiz PHP Quiz SQL Quiz

Color Picker

Statistics
Browser Statistics Browser OS Browser Display

REPORT

ERROR |

HOME

TOP

PRINT

FORUM

ABOUT

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2012 by Refsnes Data. All Rights Reserved.

3 of 3

11/12/2012 16:35

You might also like