10 Day Learning PHP (2) _php Foundation

Source: Internet
Author: User
Tags access database phpmyadmin

Third Day Learning 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 does not matter, you download a phpMyAdmin installation, after the establishment of the editing database can rely on it.

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

Then, in the left Drop-down menu, select the database that you have created. 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 the number of fields (not enough or more does not matter, later can be added or default), according to the implementation.
Then you can start building the table.
The first column is the name of the field, and 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 automatically added ID? Just select the int type and select Auto_increment on the back of the extra.

After you set up the table, you can see the table you created on the left, and after you click, you can:
1) Press the structure on the right: view the modified table structure
2) Browse to the right: view the data in the table
3 Press the SQL on the right: run the SQL statement
4) Press Right insert: Insert a row record
5) to the right of the empty: delete all records in the table
6) Press the Delete on the right: delete the table

There is also a very important function is to import and export, when we have done 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 it is SQL Server can also connect remote server to import. So in my SQL you can export all the SQL statements, to the remote server phpMyAdmin, the database after the creation of SQL, sticky paste you just copied down all the generated SQL statements. That's it for today. Database operations tomorrow. Fourth day learning purposes: Learn to connect to a database

PHP is simply a library of functions, and rich functions make php quite simple in some places. I suggest everyone down a PHP function manual, the total use of.

I'm just going to say a little bit here. Connect 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 stroke, 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, representing which port is used to connect to MySQL. Of course, when using a database, using Mysql_close () to turn off the connection early 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. Returns true successfully, and returns False if failed.

The simplest example is:
$conn =mysql_connect ("127.0.0.1", "" "," ");
mysql_select_db ("Shop");
Connect my SQL database and open the shop database. In practical application, we should strengthen the point error judgment.

Let's talk about it today. Read the database again tomorrow.
The fifth day study purpose: Learn to read data

Look at two functions first:
1, mysql_query
Send out a query string. Syntax: int mysql_query (string query, int [link_identifier]); Return value: Integer This function sends a query string for MySQL to do related processing or execution. If you do not specify the Link_identifier parameter, the program automatically looks for the most recently opened ID. If the query string is UPDATE, INSERT, and DELETE, the return may be true or false, and the query's string is a SELECT that returns a new ID value, and when false, it is not a successful execution but no return value, but a string of queries that 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. Returns the value of False if result has no data.

Look at a simple example:
?
$exec = "SELECT * from user";
$result =mysql_query ($exec);
while ($rs =mysql_fetch_object ($result))
{
echo "Username:". $rs->username. " <br> ";
}
?>
Of course, there is a username field in 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 as not rs.eof
Response.Write "Username:" &rs ("username") & "<br>"
Rs.movenext
Loop
%>
Of course, we have to connect the database first, generally we require_once (' conn.php '), and conn.php inside is the last time the code that connects the database.

The small two commands can complete the work of reading the data, said here today the next time to say that the data added delete changes.
Sixth Day Learning purposes: Learn to add delete modify data mysql_query ($EXEC);
The single statement can perform all the operations, and the difference is $exec This SQL statement adds: $exec = "INSERT INTO tablename (ITEM1,ITEM2) VALUES ('. $_post[' item1 ']." ', '. $_ post[' item1 ']; Delete: $exec = "Delete from tablename where ..."; Modified: $exec = "UPDATE tablename set item1= '". $_post[' item1 '. "' Where ... '; Speaking of which, we'll talk about forms and PHP variable passing, if one of the forms <input name= "item1" type= "text" id= "Item1" >
The form is submitted by POST, then processing the form file can get the value of the variable with $_post[' item1 ', and the same as getting the $_get[' item1 ' is the simple one? But usually $exec can be problematic because your SQL statements may be very long, and you'll miss out. Connectors, or ' to surround character fields.
We can annotate mysql_query ($exec); statements use echo $exec instead to output $exec to check for correctness. If you are not aware of any $exec errors, you can copy the SQL statement to phpMyAdmin and see its error message. It is also important to note that we do not use sensitive strings as field names, otherwise there are likely to be problems, such as date or something. The name of the variable, the name of the field follow a little rule sometimes it's a good thing for yourself, and beginners can't ignore its importance. Here today, you can down a SQL statement reference manual, and then research. Continue to say session tomorrow.

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.