10-Day Learning PHP (2) _php tutorial

Source: Internet
Author: User
The third day study purpose: Learn to build a database

In ASP, if you are an Access database you can directly open access to edit the MDB file, if it is SQL Server you can open Enterprise Manager to edit the SQL Server database, but in PHP, MY SQL command-line editing may make beginners feel very troublesome, It doesn't matter, you download a phpmyadmin to install, and later build the editing database can rely on it.

Let's talk about its use.
After entering the phpMyAdmin, we first need to set up a database,
Language (*) Here Select Chinese Simplified, and then on the left to create a new database here fill in the database name, click Create.

Then select the database that you have created in the left drop-down menu. In the following

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

Fill in the table name and roughly what you think of the number of fields (not enough or more of it does not matter, can be added later or default), press to execute.
Then you can start building the table.
The first column is the name of the field; the second column selects the field type:
The following are some of our common uses:
1) VARCHAR, text type
2) INT, integer type
3) Float, floating-point type
4) Date, type
5) You may ask, where is the auto-added ID? As long as you select the int type, you can select Auto_increment in the additional options below.

After you have established the table, you can see the table you created on the left, and after clicking, you can:
1) Press the structure on the right: View Modify table Structure
2) Browse by right: View the data in the table
3) Press the right SQL: Run SQL statement
4) Press Insert on right: Insert a row of records
5) on the right, clear: Delete all records in the table
6) Press DELETE on the right: Delete table

There is a very important function is import and export, when we have the program and database, we need to have a local mirror on the server, if the ASP access is simple, upload the MDB file directly, if SQL Server can also connect to the remote server for import. So in my SQL you can export all the SQL statements, to the remote server phpMyAdmin, after the database is created, press SQL, paste all the SQL statements you just copied down. Today, we'll talk about database operations tomorrow. Fourth day study Purpose: Learn to connect the database

PHP is simply a library of functions, rich in functions that make some parts of Php quite simple. We recommend that you down a PHP function manual, the total use of the.

I'll just talk about it here. Connect to MySQL database.

1, Mysql_connect

Open the MySQL server connection.
Syntax: int mysql_connect (string [hostname] [:p ort], string [username], string [password]); Return value: Integer This function establishes a connection to the MySQL server. All of these parameters can be omitted. When using this function without any arguments, the default value of the parameter hostname is localhost, the default value for the parameter username is the owner of the PHP execution itinerary, and the parameter password is an empty string (that is, no password). The parameter hostname can be followed by a colon and a port number, which represents which port to use for MySQL connection. Of course, when using the database, using Mysql_close () to turn off the connection earlier can 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 the database in the MySQL server for subsequent data query jobs (query) processing. The success returns True, and False if it fails.

The simplest examples are:
$conn =mysql_connect ("127.0.0.1", "", "" ");
mysql_select_db ("Shop");
Connect your computer to my SQL database and open the shop database. In practical application, the point error judgment should be strengthened.

We'll talk about it today. Read the database again tomorrow.
Fifth Day Study Purpose: Learn to read data

Take a look at two functions first:
1, mysql_query
Sends out a query string. Syntax: int mysql_query (string query, int [link_identifier]); Return value: Integer This function sends out the query string for MySQL to do related processing or execution. If the Link_identifier parameter is not specified, the program automatically looks for the most recently opened ID. The query query string is either True or False when it is UPDATE, INSERT, and DELETE, the queried string is SELECT to return a new ID value, when False is returned, it is not successful but no return value, but the query string is wrong Miss.

2, Mysql_fetch_object return class data. Syntax: Object mysql_fetch_object (int result, int [Result_typ]); Return value: Class

This function is used to split query result results into class variables. If result has no data, a value of false is returned.

See a simple example:
$exec = "SELECT * from user";
$result =mysql_query ($exec);
while ($rs =mysql_fetch_object ($result))
{
echo "Username:". $rs->username. "
";
}
?>
Of course, there is a username field in the table user, which is similar to the one in ASP
<%
Exec= "SELECT * from User"
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open exec,conn,1,1
Do and not rs.eof
Response.Write "Username:" &rs ("username") & "
"
Rs.movenext
Loop
%>
Of course first to connect to the database, generally we require_once (' conn.php '), and conn.php inside is the last time that the connection database code.

A little two commands can complete the work of reading the data, today said the next time the addition of data to delete the modification.
Sixth Day Study Purpose: Learn to add delete modified data mysql_query ($EXEC);
Single This statement can do all the operation, the difference is $exec this SQL statement 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 ... '; Speaking of which, we need to talk about the form and the PHP variable pass, if one of the forms
The form is submitted as a POST, then processing the form file can use $_post[' item1 '] to get the value of the variable, the same as get submitted is $_get[' item1 ' is not very simple? But usually $exec can be problematic because your SQL statements may be long and you'll miss them. Connectors, or ' to surround a character field.
We can annotate mysql_query ($exec); The statement uses the Echo $exec, instead of outputting the $exec to check for correctness. If you are not aware of any errors in $exec, you can copy this SQL statement to phpMyAdmin to see if it has an error message. It is also important to note that we do not use sensitive strings as field names, or there are likely to be problems, such as date or something. The name of the variable, the name of the field follows a little regularity sometimes it is a benefit to oneself, the beginner should not neglect its importance. Today, you can down a reference manual for a SQL statement, and then study the study. Go ahead and say the session tomorrow.

http://www.bkjia.com/PHPjc/314617.html www.bkjia.com true http://www.bkjia.com/PHPjc/314617.html techarticle The third day of learning purpose: Learn to build a database in ASP, if it is an Access database you can open access directly to edit the MDB file, if it is SQL Server you can open Enterprise Manager to ...

  • 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.