Home  >  Article  >  Backend Development  >  Learn php(2) in ten days_PHP tutorial

Learn php(2) in ten days_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:09:09666browse


The third day Learning purpose: Learn 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 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 PHPMYADMIN and install it. You can rely on it to build and edit the database in the future.

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

Then select the created database in the drop-down menu on the left. Create a new table in the database shop below

:
Name:
Number of fields:

Fill in the table name and the approximate number of fields you think (not enough or too many) It doesn’t matter, you can add it later or default it), press execute.
Then you can start creating the table.
The first column is the name of the field; the second column selects the field type:
We commonly use the following ones:
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? Just select the INT type and select auto_increment in the following extras.

After creating the table, you can see the table you created on the left. After clicking, you can:
1) Press the structure on the right: View and modify the table structure
2) Press Browse on the right : View the data in the table
3) Press SQL on the right: Run the SQL statement
4) Press Insert on the right: Insert a row of records
5) Press Clear on the right: Delete all records in the table
6) Press Delete on the right: Delete table

Another very important function is import and export. When we have completed the program and database locally, we need to have a local mirror on the server. If ASP's ACCESS is simple. You can directly upload the MDB file. If it is SQL SERVER, you can also connect to the remote server for import. Then you can export all SQL statements in MY SQL, go to PHPMYADMIN on the remote server, press SQL after creating the database, and paste all the SQL statements generated by this level that you just copied. That’s all for today, and we’ll continue talking about database operations tomorrow. The fourth day Learning purpose: Learn to connect to the database

PHP is simply a function library. The rich functions make some parts of PHP quite simple. It is recommended that you download a PHP function manual so that you can always use it.

I will 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 to the MySQL server. All parameters can be omitted. When this function is used without any parameters, the default value of the hostname parameter is localhost, the default value of the username parameter is the owner of the PHP execution process, and the password parameter is an empty string (that is, there is no password). A colon and port number can be added after the parameter hostname to indicate which port to use to connect to MySQL. Of course, when using a database, using mysql_close() early to close the connection 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 operations (query) processing. Returns true on success, false on failure.

The simplest example is:
$conn=mysql_connect ("127.0.0.1", "", "");
mysql_select_db("shop");
Connect to MY SQL Database, open the SHOP database. In practical applications, error judgment should be strengthened.

That’s it for today. Let’s talk about database reading tomorrow.
fifth day Learning purpose: Learn to read data

First look at two functions:
1. mysql_query
Sends 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 will automatically look for the most recently opened ID. When the query string is UPDATE, INSERT and DELETE, the returned value may be true or false; when the query string is SELECT, a new ID value is returned. When false is returned, it does not mean that the execution is successful but there is no return value, but There is an error in the query string.

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 result into a class variable. If result has no data, a false value is returned.

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, table There is a username field in user, which is similar to
<%
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, we need to connect to the database first. Generally, we require_once('conn.php'); and conn.php contains the code to connect to the database mentioned last time.

A small two commands can complete the work of reading data. Today we will talk about adding, deleting and modifying data next time.
The sixth day Learning purpose: Learn to add, delete, and modify data mysql_query($exec);
This statement alone can perform all operations, the difference is the $exec sql statement Add: $exec="insert into tablename (item1,item2) values ​​('".$_POST['item1']."',".$_POST['item1'].")"; Delete: $exec="delete from tablename where..."; Modification: $exec="update tablename set item1='".$_POST['item1']."' where ..."; Speaking of which, we need to talk about form and php variable transfer. If an
form in the form is submitted by POST, then the form is processed You can use $_POST['item1'] to get the variable value of the file. Similarly, $_GET['item1'] is submitted with GET. Isn't it very simple? But usually $exec will have problems, because your SQL statement may be very long, and you will miss the .connection character, or ' to surround the character field.
We can comment out the mysql_query($exec); statement and use echo $exec; instead to output $exec to check the correctness. If you still can't detect any errors in $exec, you can copy this sql statement and execute it in phpmyadmin to see its error message. It should also be noted that we should not use some sensitive strings as field names, otherwise problems may occur, such as date and so on. Sometimes it is good for you to follow some rules when naming variables and fields. Beginners should not ignore its importance. That’s it for today. You can DOWN a reference manual for SQL statements and study it further. Let’s continue talking about SESSION tomorrow.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314617.htmlTechArticleThird day learning purpose: Learn to build a database in ASP. If it is an ACCESS database, you can directly open ACCESS to edit it. MDB file, if it is SQL SERVER, you can open the Enterprise Manager to...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn