Getting started with PHP MySQL: creating databases/Tables and saving data

Source: Internet
Author: User
Tags getting started with php php mysql

In mysql, if we want to use PHP to CREATE databases and tables, we can directly use mysql_query () to execute the mysql table creation command, create database database_name.

The details are as follows:

To allow PHP to execute the preceding statement, we must use the mysql_query () function. This function is used to send queries or commands to MySQL connections.

Example

In the following example, we created a database named "my_db:

The Code is as follows: Copy code

<? 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 );
?>

Create a table

Create table is used to CREATE a database TABLE in MySQL.

Example

The following example shows how to create a table named "Persons", which has three columns. The column names are "FirstName", "LastName", and "Age ":

 

The Code is as follows: Copy code
<? 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 ))
{
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 );
?>

Insert data to a database table

You can also specify the columns in which you want to insert data:

Insert into table_name (column1, column2 ,...)
VALUES (value1, value2 ,....)

Example

In the previous section, we created a table named "Persons" with three columns: "Firstname", "Lastname", and "Age ". We will use the same table in this example. The following example adds two new records to the "Persons" table:

The Code is as follows: Copy code
<? Php
$ Con = mysql_connect ("localhost", "peter", "abc123 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}
Mysql_select_db ("my_db", $ con );
Mysql_query ("insert into Persons (FirstName, LastName, Age)
VALUES ('Peter ', 'Griffin', '35 ')");
Mysql_query ("insert into Persons (FirstName, LastName, Age)
VALUES ('glenn', 'quagmire', '33 ')");
Mysql_close ($ con );
?>

--------------------------------------------------------------------------------
Insert data from the form into the database

The Code is as follows: Copy code

<Html>
<Body>
<Form action = "insert. php" method = "post">
Firstname: <input type = "text" name = "firstname"/>
Lastname: <input type = "text" name = "lastname"/>
Age: <input type = "text" name = "age"/>
<Input type = "submit"/>
</Form>
</Body>
</Html>

When you click the submit button in the HTML form in the preceding example, the form data is sent to "insert. php "." The insert. php file connects to the database and retrieves the value from the form using the $ _ POST variable. Then, the mysql_query () function executes the insert into statement, and a new record is added to the database table.

The code for the "insert. php" page is as follows:

 

The Code is as follows: Copy code
<? Php
$ Con = mysql_connect ("localhost", "peter", "abc123 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}
Mysql_select_db ("my_db", $ con );
$ SQL = "INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$ _ POST [firstname]', '$ _ POST [lastname]', '$ _ POST [age]') ";
If (! Mysql_query ($ SQL, $ con ))
{
Die ('error: '. mysql_error ());
}
Echo "1 record added ";
Mysql_close ($ con)
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.