Introduction to Dynamic Web Technology PHP Basics: Start programming

Source: Internet
Author: User
Tags chr mysql variables php file php basics mysql database

First, the analysis before programming

To make a message board, and we also decided to do it in a database way, so we start with the following steps:

1. Fill in the message

Where do you write your message?

2. Send Message

When you enter data in a text box, you are sure to upload it in what way, and then the file receives the variable.

3. Into the database

The last file will receive the data to enter the database, so we have to master so to write data to the database.

4. View Results

After writing the message to the database, it is mainly for viewing in the browser, so we need to know how to read the data from the database.

Second, make a fill in the message file (write.htm)

<form action= "write_ok.php" method= "POST" >

Please enter your nickname: <input type= "text" name= "nickname" ><br>

Please leave a message: <textarea name= "Note" ></textarea><br>

<input type= "Submit" value= "OK" >

</form>

Three, PHP variable transfer

A friend who is familiar with ASP may know that after transferring a variable from one file to another, the second file is the request to receive data; PHP seems simpler, and when a file sends a variable to another file, it automatically generates a variable, and I'll give you an example of ASP and PHP, respectively.

1.ASP:

Form.htm:

<form action= "asp_to.asp" method= "POST" >

Enter nickname: <input type= "text" name= "nickname" >

<input type= "Submit" value= "OK" >

</form>

Asp_to.asp:

The nickname you have entered is: <% Response.Write (Request.Form ("nickname"))%>

Simple description: In the first file, there is a text box, named nickname and a OK button, in the text box after entering information, click "OK", the browser will be submitted to asp_to.asp the file will you in the text box to display the content, Response.Write is the display output, and the request is to receive the variable "nickname".

2.PHP:

Form.htm:

The file is the same as the above, except that the asp_to.asp is changed to php_to.php, which is to submit the input variable to a PHP file named php_to.php.

php_to.php:

The nickname you have entered is: echo $nickname;?>

So, where echo is the display output, equivalent to Response.Write in ASP and $ for variables, PHP variables are represented by $.

Iv. PHP Operations Database

In the previous section, I introduced the MySQL database and the creation of datasheets. Below, a simple example of how PHP operates the database is presented. [Example One]

?

$id =mysql_connect (' localhost ', ' root ', ' AdminPassword ');

$db =mysql_select_db (' Gsbook ', $id);

$result =mysql_query (' select * from Gsbook ', $db);

$maxrows =mysql_num_rows ($result);

for ($i =0; $i < $maxrows; $i + +)

{

$nickname [$i]=mysql_result ($result, $i, ' nickname ');

}

for ($i = $i -1;$>=0; $i-)

{

echo "nickname:". $nickname [$i]. " <br> ";

}

?>

Simple description:

1. function int mysql_connect (string [hostname] [:p ort],string [username],string [password]) returns a MySQL connection ID, whose parameters are believed to be known as soon as you see it.

2. function int mysql_select_db (string database_name, int [link_identifier]) is used to open the database, the first function is the database name, the second is the MySQL connection ID, The return value is Ture or false

3. function int mysql_query (string query, int [link_identifier]) returns a result

4. function int mysql_num_rows (int result) returns the number of rows in query.

5. function int mysql_result (int result,int row,mixed field) This function returns the result of a field in a row, the second argument is the number of rows, and the third argument is the field name, and it should be stated that the field name is not the physical field name, for example: Select Field1 as Alias_field from Table1 then, the third argument in the function must be alias_field instead of field1.

Circular statements in 6.PHP:

(1) do ... while

(2) while ... [End While]

(3) for (EXPR1;EXPR2;EXPR3)

The same as C, not in detail here.

7. In the example above, two for loop statements were used, the first to read the values in the database into an array, and the second to display the values in the array. Note the second for loop "$i = $i-1;" This is because the value of the variable $i has been added 1 since the first loop, so when you take a value from an array, you should subtract 1.

The addition of strings in 8.PHP is ".", equivalent to "+" or "&" in ASP (VB syntax).

9. So, the function of the above code is to read out the data from the database and show it.

Next, we'll add a record to the database, first look at the code [example two]:

?

$id =mysql_connect (' localhost ', ' root ', ' AdminPassword ');

$db =mysql_select_db (' Gsbook ', $id);

$sql = ' INSERT into Gsbook values ('. chr () $nickname. chr (39). ') ';

if (mysql_query ($sql, $db) >0) {

echo "OK";

}

else {

echo "failed!";

}

?>

Simple description:

1. For familiar with the SQL statement of friends, look at the simple introduction of a case after the function of the second part must know.



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.