PHP read data from the database implementation method _php tutorial

Source: Internet
Author: User
Tags echo date mysql manual
This article makes use of a simple example to implement PHP to read the data from the database in detail, there is a need for friends to see, here at the same time also simply talk about security issues.

First look at the segment code

The code is as follows Copy Code

Session_Start ();
$con =mysql_connect (' localhost ', ' root ', ' root ') or Die (' Link database failed! ');
mysql_query (' Set names UTF8 ');
mysql_select_db (' guestbook ');

$pagesize = 10;//How many message records are displayed on each page
if (Isset ($_get[' page ')) &&$_get[' page ']!= ') $page =$_get[' page '];
else $page = 0;

$sql = "Select a. *, B.name, B.email, B.qq, C.revert_time, C.revert
From Post a
Left JOIN revert c on (a.id = c.post_id), Guest B
WHERE a.guest_id = b.id
ORDER by a.ID DESC ";
$numRecord = Mysql_num_rows (mysql_query ($sql));
$totalpage = Ceil ($numRecord/$pagesize);

$RECORDSQL = $sql. "LIMIT". $page * $pagesize. ",". $pagesize;
$result = mysql_query ($RECORDSQL);
?>

Here's a look.

The first line: We do not say for the time being, stay until later, and temporarily still not use;
The second line--The fifth line: Everyone must be familiar with it, yes here and post.php Insert message information when the process is the same, also do not say much.
Lines 7, 8, 9, and 19 we don't say for the moment that this is about paging, and we'll leave it in the next chapter to explain the problem in detail, because the paging process is almost a must-have feature in the Web programming process.

In line 11-15 We define a database query statement in which we use the LEFT join ... on. Statement, for a detailed usage of this statement, refer to the MySQL Manual Joint query section.

Look at line 16th,

The code is as follows Copy Code

$numRecord = Mysql_num_rows (mysql_query ($sql));

In this statement we use mysql_query ($sql) to send our defined SQL statements to the database to execute, this statement will return a record resource number after execution, we use the outer mysql_num_rows to get how many matching records-that is, the total number of records returned.

Line 17th We used the Ceil function, the function is, into a method to take the whole, that is, two numbers after the division operation as long as the remainder is greater than the zero integral part of the addition and then the fractional portion, so ceil (1/2) The result is 1, not what we usually see 0.5. This is very useful for us--and let's say it in the next chapter of the page.

The 19th line relates to the pagination section. But this line is worth saying, in PHP, the addition of strings, that is, the connection of strings. The operator of the two string addition is the point, see Example:

The code is as follows Copy Code

$strFirst = "China";
$strTwo = "Great Kingdom";

$strSum = $strFirst. $strTwo;
Echo $strSum;

The result of the output is the same as we expected: The great country of China.

Line 20th: We send an SQL statement with paging function to the database execution, and assign the execution result to $result;
Then we look at this page of the function of the line: 128-154, the fragment function of this program is the loop output in the database query to the record. This is the focus of our chapter:

Look at the PHP built-in function mysql_fetch_object () from the result set to get a row as an object, about the object of PHP in the Advanced Programming section will be described in detail, we only need to know this circular query record of the way, need to use the $ object--by access to the line , which is performed every time the pointer is moved down once, when there is no more records to return a false so we can use the while loop to read the $result recordset. Finally form the while ($rs =mysql_fetch_object ($result)) This statement, loop calls the Mysql_fetch_object function and assign each returned object to the $rs variable, now oh we look at the inside of this loop, we can use $rs->name to get the name of the message, with $rs->post_time to access the message time.

We did some processing in the display of the Post_time, using the date () function, remembering that we used the time () function when we stored the message, which returned a timestamp of the number of seconds (GMT) that had elapsed since the first second of 1970. The date function is to translate this number of seconds into the time format we need, and the return string needs to be set in the first argument of the date () function, and if the second argument of the function is not given, that is, the number of seconds that need to be translated into the time format is not given, he will translate it in the current time.
Date ("Y year M D Day H:i:s"), the output is April 26, 2009 11:16:20.

We have this notation in the 132 lines of index.php:

The code is as follows Copy Code
post_time+8*3600)?>

We have specifically said this, I think we will certainly be 8*3600 this place is strange, in fact, the reason is very simple. We use the time () function to store times with Greenwich Mean Time, which is exactly eight hours with our time, 3,600 seconds per hour, we are in the east faster than the West, so we have to add. Remember we use Beijing time-in the East eight time zone Oh, this is common sense! There is another way to do this, which will be explained in more detail in the article dedicated to the date function of PHP.

Lines 144 and 146 use two functions htmlspcialchars and NL2BR, where htmlspcialchars is necessary to avoid the need for user-entered script code (HTML and JavaScript) to be executed, and this function is important. Without this function the user input JS code and HTML code will be executed as part of the page program, which we have said in the article on PHP code security recommendations, not to repeat.
The function of NL2BR is to translate the N-line in the data into the wrapping of the HTML.
To facilitate page performance. When we enter the message information, the use of this form, the time to save the information, the line to use N to express, so we need to convert to keep the user input information at the time of display I input the paragraph consistent.

http://www.bkjia.com/PHPjc/631301.html www.bkjia.com true http://www.bkjia.com/PHPjc/631301.html techarticle This article makes use of a simple example to implement PHP to read the data from the database in detail, there is a need for friends to see, here at the same time also simply talk about security issues. First look at the segment generation ...

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