How to dynamically extract data from the database on the php entry page

Source: Internet
Author: User
Zhang Shumei, Computing Center of Shijiazhuang Normal College abstract: before submitting the server, it is a difficulty in PHP program development to allow PHP to immediately retrieve and display the values of other related fields from the database based on the values of a field entered on the current page. This document describes how to embed two HTML files into the computing center of Shijiazhuang Normal College, Zhang Shumei.

Abstract: when using PHP to create a dynamic Web page, before submitting the page to the server, it is a difficulty in PHP program development to allow PHP to immediately retrieve and display the values of other related fields from the database based on the values of a field entered on the current page. This article describes in detail how to combine PHP and JavaScript, two embedded HTML languages, to solve this difficulty.

Keywords: PHP, dynamic, HTML.

The current website has evolved from providing static information to interactive dynamic information services. The Web information service can be summarized into two forms: providing information to customers and recording information submitted by customers. To provide these two services, you need to solve the problem: how to quickly allow users to quickly extract the information they want from a large amount of information on their websites, how to effectively record information submitted by users for future user searches. These problems can be solved by adding database support to the website.

Because PHP supports a variety of databases, and PHP scripts are directly embedded in HTML documents, it is very convenient to use. Therefore, PHP is one of the most popular Server-side embedded languages on the Internet. In addition, compared with other Server-side scripting languages such as ASP, PHP provides free open source code and cross-platform support, which makes it easy to adapt to various heterogeneous network environments in today's network; this allows Web page producers to quickly and conveniently create powerful dynamic Web pages. However, because PHP is embedded on the server, it is more intuitive to understand that PHP statements are executed on the server, so it receives and processes the content on the current page only when it is submitted. PHP is powerless when you need to dynamically extract data from the database based on the value of a field entered on the current page of the customer. For example, to provide a "order contract" entry page to the customer, which contains some "supplier information" entry, the detailed information of each supplier has already been input in a "seller" dictionary table. now, when the customer selects a "supplier" on the current page, some information of the supplier, such as the "opening bank, account, address, and phone number", is extracted from the "merchant" dictionary table and displayed on the current page for direct use or modification by the customer. This requirement is easy to implement with visual programming languages such as PB and VB, but PB and VB are not suitable for compiling dynamic Web pages. PHP is suitable for compiling dynamic Web pages, however, because it is embedded on the server, the variable values on the page before submission cannot be obtained in time, so it is difficult to implement the above requirements. During programming, I skillfully combined PHP with JavaScript to solve this difficulty.

We know that it is also an embedded statement, but JavaScript is different from the PHP language. Because PHP is embedded on the server, while JavaScript is embedded on the client, JavaScript statements are executed on the client's browser, which determines that JavaScript can obtain the variable values on the current page in a timely manner, however, you cannot directly operate the database on the server. Therefore, the combination of the two is a perfect combination of dynamic Web pages. For ease of description, the following describes how to retrieve the address of the selected supplier from the dictionary. When you need to retrieve multiple fields, the method is similar, but when using JavaScript functions to extract them from strings one by one, you should be careful.

1. compile a PHP function

This function extracts all qualified "supplier information" from the "seller" dictionary and stores it in a string variable $ khsz.

Function Khqk_Tq ($ questr ){

Global $ dbconn;

$ Dbq_resl = sybase_query ($ questr, $ dbconn); // sends a query string for Sybase to execute.

$ Dbq_rows = sybase_num_rows ($ dbq_resl); // Obtain the number of returned rows.

$ J = 0;

For ($ I = 0; $ I <$ dbq_rows; $ I ++) {// create a loop to retrieve the required fields in each row and put them in the array $ k.

$ K [] = sybase_result ($ dbq_resl, $ I, "kh_id"); // Retrieve the supplier ID selected by the user.

$ Add = sybase_result ($ dbq_resl, $ I, "address"); // Obtain the supplier address.

If ($ add = ""):

$ K [] = "none ";

Else:

$ K [] = sybase_result ($ dbq_resl, $ I, "address ");

Endif;

$ Khsz = $ khsz. $ k [$ j]. "| ". $ k [$ j + 1]. "|"; // separate the field values with "|" and connect them to the variable $ khsz to form a long string.

$ J = $ j + 2;

}

Return $ khsz;

}

2. compile a JavaScript function

The function is to find the address of the supplier based on the kh_id value in the string and embed it into the HTML file.

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.