PHP MySQL Create Database and Tables
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
w3schools.com
on Facebook
Next Chapter
Suka
20rb
Create a Database
The CREATE DATABASE statement is used to create a database in MySQL.
Syntax
CREATE DATABASE database_name
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
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":
1 of 3
11/12/2012 16:35
http://www.w3schools.com/php/php_mysql_create.asp
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
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
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
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