How to implement php to read data from the database_PHP tutorial

WBOY
Release: 2016-07-13 16:59:40
Original
999 people have browsed it

The article uses a simple example to implement PHP to read data from the database and explains it in detail. Friends in need can take a look. It also briefly talks about security issues.

Look at the code first

The code is as follows Copy code
 代码如下 复制代码

session_start();
$con=mysql_connect('localhost','root','root') or die('链接数据库失败!');
mysql_query('set names utf8');
mysql_select_db('GuestBook');

$pagesize = 10;//每一页显示多少留言记录
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);
?>

session_start();

$con=mysql_connect('localhost','root','root') or die('Failed to connect to database!');

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
代码如下 复制代码

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

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);

代码如下 复制代码

$strFirst=”中国”;
$strTwo=”伟大的国度”;

$strSum = $strFirst.$strTwo;
echo $strSum;

$recordSql = $sql. " LIMIT ".$page*$pagesize.",".$pagesize;<🎜> $result = mysql_query($recordSql);<🎜> ?>
Let’s take a look The first line: We won’t talk about it for now, we will leave it to later, it is not needed yet; Line 2 - Line 5: It must look familiar to everyone. Yes, the process here is the same as when inserting message information in post.php, so I won’t go into details. We won’t talk about lines 7, 8, 9 and 19 for now. This involves paging. We will leave it to the next chapter to explain this issue in detail, because the paging process is almost a must in the process of web programming. Function. In lines 11-15 we define a database query statement. In this statement we use the left join...on.. statement. For detailed usage of this statement, please refer to the joint query section of the Mysql manual. . Look at line 16,
The code is as follows Copy code
$numRecord = mysql_num_rows(mysql_query($sql));
In this statement, we use mysql_query($sql) to send the sql statement we defined to the database for execution. After this statement is executed, a record resource number will be returned. We use the outer mysql_num_rows to get how many matching records there are. ——That is, the total number of records returned. We use the ceil function in line 17. The function of this function is to round to the nearest integer. That is to say, after dividing two numbers, as long as the remainder is greater than zero, the integer part is added by one and then the decimal part is discarded, so The result of ceil(1/2) is 1, not the 0.5 we usually see. This is very useful for us - let's leave it to the pagination of the next chapter. Line 19 involves the paging part. But what is worth mentioning about this line is that the addition of strings in PHP is the concatenation of strings. The operator for adding two strings is dot, see example:
The code is as follows Copy code
$strFirst=”China”; $strTwo=”Great Country”; $strSum = $strFirst.$strTwo; echo $strSum;

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

Line 20: We send a sql statement with paging function to the database for execution, and assign the execution result to $result;
Next, let’s look at lines 128-154 of the function on this page. The fragment function of this program is to loop out the records queried in the database. This is the focus of our chapter:

Look at PHP’s built-in function mysql_fetch_object() to obtain a row as an object from the result set. We will explain in detail about PHP objects in the advanced programming column. We only need to know this way of looping query records. We need to use $Object->Field name is enough to access. When executed, the pointer moves down every time. When there are no more records, a false is returned, so we can use a while loop to read $result. this record set. Finally, the statement while($rs=mysql_fetch_object($result)) is formed. The mysql_fetch_object function is called in a loop and the object returned each time is assigned to the $rs variable. Now let’s look at the inside of this loop. We can use $rs-> ;name to get the name of the person who left the message, and $rs->post_time to access the time of the message.

We did some processing when displaying post_time, using the date() function. Remember that we used the time() function when storing messages. This function returns a timestamp, which is from the first date in 1970. Seconds to the number of seconds that have elapsed so far (GMT). The date function translates this number of seconds into the time format we need. What kind of string is returned needs to be specified in the first parameter of the date() function. If the second parameter of the function is not given, it will also If the number of seconds that needs to be translated into time format is not given, it will be translated based on the current time.
Date("Y year m month d day H:i:s"), the output result is April 26, 2009 11:16:20.

We have this writing on line 132 of index.php:

Let’s talk about this in detail. I think everyone will be surprised by the place 8*3600. In fact, the reason is very simple. When we use the time() function to store time, we use Greenwich time. This time is exactly eight hours different from our time, and each hour is 3600 seconds. We are in the east and time is faster than the west, so we have to add it. Remember we are using Beijing time - we are in the East Eighth Time Zone. This is common sense! There is another way to do this, which will be explained in detail in the article dedicated to PHP's date function.
The code is as follows
 代码如下 复制代码
post_time+8*3600)?>
Copy code

post_time+8*3600)?>


Lines 144 and 146 both use two functions htmlspcialchars and nl2br. The htmlspcialchars function is necessary to prevent the script code (html and javascript) entered by the user from being executed. This function is very important. Without this function, the js code and html code input by the user will be executed as part of the page program. We have already said this in the article "Recommendations on PHP Code Security" and will not go into details.

The function of nl2br is to convert n line breaks in the data into HTML line breaks
to facilitate page performance. When we enter message information, we use the form . When saving the information, line breaks are represented by n, so we need to convert here to retain the user input information. I enter it when it is displayed. The paragraphs are consistent.

http://www.bkjia.com/PHPjc/631301.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/631301.htmlTechArticle
The article uses a simple example to implement PHP to read data from the database and explain it in detail. Friends in need can Look, here we also briefly talk about security issues. Let’s look at Duan Dai first...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template