Basic syntax for MySQL statements in PHP

Source: Internet
Author: User

Basic syntax for MySQL statements in PHP

PHP as a background language must be dealing with the MySQL database, to store the content to the database and database data read and write operations, then the following is the next thing to learn:

Before the specific will say the coding problem, a lot of things will find that PHP content display in the browser will have coding problems, and the foreground stored in the database table in the text message will also appear garbled problem, then there are several places to note:

1, header ("Content-type:text/html;charset=utf-8");//forcibly specify the encoding of the page to avoid garbled

In PHP before inserting this sentence, you can avoid PHP in the page garbled

2, Engine=myisam auto_increment=33 DEFAULT charset=utf-8 This sentence is in the creation of the database table, the end of a sentence, you can avoid the database read garbled problem

3, after mysql_select_db () add a sentence mysql_query ("Set names ' UTF8 '"); can avoid writing database garbled problem

MySQL data read and write, divided into the following sections:

1. Link Database

$con =mysql_connect ("localhost", "root", "");

The above sentence is to create a MySQL connection, while trying to connect to localhost, which is 127.0.0.1, while accessing the database, the account is: root, password is empty, we can make some judgments to check whether the database link is successful

if (! $con) {

Die (' Database link failed '. mysql_error ());

}else{

Echo ' database link succeeded ';

}

2. Creating a Database

Execute a database statement by using the mysql_query () method

mysql_query ("CREATE Database Phpone", $con), where $con is a link identifier

So through the above, we created a database called Phpone, and we can see the database we created by Navivat and other database Tools.

3, the light has a database is not enough, we need a database of various tables, then this step is to create a database table (divided into two steps)

First: Before you create a database table, we need to select the database that you created, possibly with a lot of songs on your server.

mysql_select_db ("Phpone", $con);

Part II: Create a database table, because the database statements may be more, we can be assigned to write the way

1 $sql="CREATE TABLE Personinfo (2  3 PersonID int NOT NULL auto_increment,//creates a primary key, as a unique identifier, for integer, auto-Grow4  5 primary KEY (PersonID),//Set Primary key to PersonID field name6  7 name varchar (15),//set name to a character with a variable size of 15 characters8  9 Age int,//defines an age field as an integral typeTen   One regist date//definition regist is a date type, ps The last end of no comma A   -) Engine=myisam auto_increment=33 DEFAULT charset=utf-8 ";

mysql_query ($sql, $con);

So, with the method above, we'll create a database table

4. Deposit data into the database

mysql_query (INSERT into Personinfo (name,age,regist) VALUES ("Xiao Li", ' 23 ', ' 2012-09-26);//So through this sentence, we have successfully inserted a data in the database table

So in fact, through these explanations, a basic form of the registration information submitted, while stored in the specified database table implementation process is basically presented, you can go to test

$_post[' name '] $_post[' age '] $_post[' regist '], by inserting the three data into the statement of the database, each time when the click Submit Submit button is Oh Hu will insert a message in the specified database, actually can do better, For example, many users do not like to fill in the registration time, then these things

Background database can be generated for him, by passing a $data=date ("y-m-d"), you can achieve a simple auto-fill registration time

Basic syntax for MySQL statements in PHP

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.