Dynamic Web page Technology--cgi:asp:jsp:php (4)

Source: Internet
Author: User
Tags html tags connect mysql prev reset resource mysql database linux
4. PHP Technology
I am most familiar with PHP, but also use the most. PHP----Hypertext Preprocessor (hypertext preprocessor) is a server-side scripting language that is easy to learn and use, and is one of the tools for generating dynamic Web pages. It is a scripting language for embedding HTML files. Most of the seven grammars are borrowed from the C,java,perl language and form their own unique style; The goal is to get web programmers to quickly develop dynamic Web pages. It's the hottest scripting language on the Internet today, and you can use PHP to create a truly interactive web site with very little programming knowledge.
PHP is completely free, unrestricted access to the source code, can even add to the characteristics you need. PHP can be run on most Unix platforms, Gun/linux and Microsoft Windows platforms. The official website of PHP is: http://www.php.net.
Like ASP, JSP, PHP can also be combined with HTML language, it is very good compatibility with the HTML language, users can directly in the script code to add HTML tags, or in the HTML tag to add script code to better achieve page control, provide more rich functionality.
The advantages of PHP are: Easy to install simple learning process, convenient database connection, strong compatibility, strong scalability, can be object-oriented programming. The quote from Nissan's Xterra is that PHP can do what you want it to do and do everything!
PHP provides a standard database interface that can connect almost all of the databases, especially with the MySQL database. The following is a reference to an example of a call to MySQL database and page display to deepen your understanding of PHP.
?
$pagesize = 5; Display 5 records per page
$host = "localhost";
$user = "User";
$password = "PSW";
$dbname = "book"; The name of the library table queried;
Connecting to the MySQL database
Mysql_connect ("$host", "$user", "$password") or Die ("Unable to connect to the MySQL database server!") ");

$db = mysql_select_db ("$dbname") or Die ("Unable to connect to the database!") ");

$sql = "SELECT count (*) as Total from pagetest";//SQL statement to generate query records
$rst = mysql_query ($sql) or Die ("Unable to execute SQL statement: $sql!") "); Number of query records
$row = mysql_fetch_array ($rst) or Die ("No More records!") "); /Take out a record
$rowcount = $row ["Total"];//out the number of records
Mysql_free_result ($rst) or Die ("Unable to release the result resource!") "); Releasing a result resource

$pagecount = Bcdiv ($rowcount + $pagesize-1, $pagesize, 0);/figure out a total of a few pages

if (!isset ($pageno)) {
$pageno = 1; The default is to display page 1th when no pageno is set
}
if ($pageno <1) {
$pageno = 1; If the pageno is smaller than 1, set it to 1.
}
if ($pageno > $pagecount) {
$pageno = $pagecount; If the PageNo is larger than the total number of pages, set it to the last page
}
if ($pageno >0) {
$href = Eregi_replace ("%2f", "/", UrlEncode ($PHP _self))//Convert $php_self to a string that can be used on the URL, so that you can handle the Chinese directory or the Chinese file name
if ($pageno >1) {//Show walks on previous page
echo "<a href=" ". $href. "? pageno=". ($pageno-1). "" > Prev </a>;
}
else{
echo "Prev";
}
for ($i =1; $i < $pageno; $i + +) {
echo "<a href=" ". $href. "? pageno=". $i. ">". $i. "</a>";
}
Echo $pageno. " ";
for ($i + +; $i <= $pagecount; $i + +) {
echo "<a href=" ". $href. "? pageno=". $i. ">". $i. "</a>";
}
if ($pageno < $pagecount) {//walks on next page
echo "<a href=" ". $href. "? pageno=". ($pageno + 1). "" > next page </a>;
}
else{
echo "Next page";
}

$offset = ($pageno-1) * $pagesize//figure out where the first record of this page is in the entire table (first record is 0)
$sql = "SELECT * from Pagetest LIMIT $offset, $pagesize"//SQL statement to generate query data on this page
$rst = mysql_query ($sql);//query data on this page
$num _fields = Mysql_num_fields ($rst);//Get total number of fields
$i = 0;
while ($i < $num _fields) {//Get the name of all fields
$fields [$i] = Mysql_field_name ($rst, $i);//Get the first name of I+1 field
$i + +;
}
echo "<table border=" 1 "cellspacing=" 0 "cellpadding=" 0 ">";//Start Output table
echo "<tr>";
Reset ($fields);
while (list (, $field _name) =each ($fields)) {//Display field name
echo "<th> $field _name</th>";
}
echo "</tr>";
while ($row =mysql_fetch_array ($rst)) {//Display this page data
echo "<tr>";
Reset ($fields);
while (list (, $field _name) =each ($fields)) {//display the value of each field
$field _value = $row [$field _name];
if ($field _value== "") {
echo "<td> </td>";
}
else{
echo "<td> $field _value</td>";
}
}
echo "</tr>";
}
echo "</table>";//Table Output end
Mysql_free_result ($rst) or Die ("Unable to release the result resource!") ");/release result resource
}
else{
echo "There is currently no data in the table!" ";
}

Mysql_close ($server) or Die ("Unable to disconnect from the server!") ");/disconnect and release resources
?>
From this example, we can see that the syntax structure of PHP is very much like C language and easy to master. And the cross-platform nature of PHP allows programs to run smoothly on both Windows platforms and Linux and UNIX systems. I write PHP program is in the WINNT4, and then uploaded to the UNIX system run, never found compatibility problems.
So far, no matter in the personal website or on the enterprise website, the above 4 kinds of technology are most widely used in PHP.
All of the above 4 technologies are recount on the production of Dynamic Web pages. The choice of which technology depends on the producer's hobby and technical reserve. For the vast number of personal homepage enthusiasts, producers, I suggest that as little as possible with more difficult, slow CGI technology. If you are a "Microsoft" fan, the use of ASP technology will make you handy; If you are a Linux suitor, using PHP technology is the most appropriate and sensible option right now. In addition, do not overlook the JSP technology, it is said to be the most promising future dynamic web technology, but before learning JSP, must master Java technology.


Related Article

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.