Ten days to learn php (2)

Source: Internet
Author: User
Ten days to learn php (2) third day objective: to learn how to build a database

In ASP, if it is an ACCESS database, you can directly open ACCESS to edit the MDB file. if it is an SQL server, you can open the Enterprise Manager to edit the SQL SERVER database, but in PHP, the command line editing of my SQL may be very troublesome for beginners. it doesn't matter. you can download and install PHPMYADMIN, and you can rely on it to create and edit a database later.

The following describes how to use it.
After entering phpmyadmin, we first need to create a database,
Select simplified Chinese Language (*), create a new database on the left, enter the database name here, and click create.

Select the created database from the drop-down list on the left. In the following

Create a new table in the database shop:
Name:
Number of fields:

Enter the table name and the approximate number of fields you think (it doesn't matter if it is not enough or more, you can add it later or use the default value), and click execute.
Then you can create a table.
The first column is the field name, and the second column selects the field type:
We usually use the following:
1) VARCHAR, text type
2) INT, integer type
3) FLOAT, floating point type
4) DATE, DATE type
5) You may ask, where is the automatically added ID? You only need to select the INT type, and select auto_increment in the additional section below.

After creating a table, you can see the table you created on the left. after clicking it, you can:
1) view and modify the table structure by structure on the right
2) click browse on the right: view the data in the table
3) press SQL on the right to run the SQL statement.
4) insert to the right: insert a row of records
5) clear by right: delete all records in the table
6) delete the table on the right: delete the table

Another important function is import and export. when the program and database are ready on the local machine, a local image is required on the server. if asp access is simple, directly upload the MDB file. if it is an SQL SERVER, you can connect to the remote SERVER for import. In my SQL, you can export all the SQL statements to PHPMYADMIN on the remote server. after creating the database, press SQL and paste all the SQL statements generated at the current level that you just copied. Let's talk about database operations today. Objective: to learn how to connect to a database

PHP is simply a function library. rich functions make some parts of PHP quite simple. We recommend that you use a PHP function manual.

Let me briefly talk about connecting to the MYSQL database.

1. mysql_connect

Open the MySQL server connection.
Syntax: int mysql_connect (string [hostname] [: port], string [username], string [password]); return value: integer this function establishes a connection with the MySQL server. All parameters can be omitted. When you use this function without adding any parameters, the default value of the hostname parameter is localhost, the default value of the username parameter is the owner of the PHP route execution, and the parameter password is a null string (that is, no password ). The hostname parameter can be followed by a colon and a port number, indicating which port is used to connect to MySQL. Of course, when using the database, you can use mysql_close () to turn off the connection earlier to save resources.

2. mysql_select_db

Select a database.
Syntax: int mysql_select_db (string database_name, int [link_identifier]); return value: integer

This function selects a database on the MySQL server for subsequent data query job (query) processing. True is returned for success, and false is returned for failure.

The simplest example is:
$ Conn = mysql_connect ("127.0.0.1 ","","");
Mysql_select_db ("shop ");
Connect to my SQL database and open the SHOP database. In practical applications, we should strengthen the judgment of point errors.

Let's talk about this today. let's talk about database reading tomorrow.
Day 5 objective: to learn to read data

Let's first look at two functions:
1. mysql_query
Returns a query string. Syntax: int mysql_query (string query, int [link_identifier]); return value: integer this function sends the query string for MySQL to perform related processing or execution. If the link_identifier parameter is not specified, the program automatically searches for the recently opened ID. If the query string is UPDATE, INSERT, or DELETE, the return value may be true or false. if the query string is SELECT, a new ID value is returned. if the return value is false, the query string is incorrect, instead of running successfully but without returning values.

2. mysql_fetch_object returns class data. Syntax: object mysql_fetch_object (int result, int [result_typ]); return value: Class

This function is used to split the query result into class variables. If the result contains no data, false is returned.

Let's look at a simple example:
$ Exec = "select * from user ";
$ Result = mysql_query ($ exec );
While ($ rs = mysql_fetch_object ($ result ))
{
Echo "username:". $ rs-> username ."
";
}
?>
Of course, the table user has a username field, which is similar
<%
Exec = "select * from user"
Set rs = server. createobject ("adodb. recordset ")
Rs. open exec, conn, 1, 1
Do while not rs. eof
Response. write "username:" & rs ("username ")&"
"
Rs. movenext
Loop
%>
Of course, you need to connect to the database first. generally, we require_once ('Conn. php'), while conn. php contains the code used to connect to the database.

Two small commands can be used to read data. today we will talk about how to add, delete, and modify data.
Objective: to learn how to add, delete, and modify data mysql_query ($ exec );
This statement can execute all the operations. The difference is that the $ exec SQL statement is added: $ exec = "insert into tablename (item1, item2) values ('". $ _ POST ['item1']. "',". $ _ POST ['item1']. ")"; delete: $ exec = "delete from tablename where... "; modify: $ exec =" update tablename set item1 = '". $ _ POST ['item1']. "'Where... "; here we will talk about the transfer of form and php variables. if one of
If the form is submitted in POST mode, you can use $ _ POST ['item1'] to obtain the variable value for processing the form file, similarly, $ _ GET ['item1'] is submitted with GET, isn't it easy? But usually $ exec will have a problem, because your SQL statement may be very long, you will miss the. connector, or 'to enclose the character field.
Note mysql_query ($ exec); the statement uses echo $ exec; instead of $ exec to output $ exec to check whether the statement is correct. If you do not notice any error in $ exec, you can copy the SQL statement to phpmyadmin to check its error information. Note that we should not use sensitive strings as field names. otherwise, problems may occur, such as date. Variable naming: Field naming follows a certain rule. sometimes it is good for you. beginners cannot ignore its importance. Let's talk about it today. you can go DOWN a reference manual for SQL statements and study it again. Continue to talk about the SESSION tomorrow.
Related Article

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.