PHP Scripting Database features 1_php tutorial

Source: Internet
Author: User
Tags php database
(Author: Wang Kaibo) in the current rapid development of the Internet, e-commerce sites in endless situation, the Web site development efficiency and quality has put forward more and more high requirements. For large-scale and complex structure, the content of a wide range of sites, to achieve the dynamic and convenient site management. Data management is inseparable from database system support.   And the important symbol of measuring a CGI language is its access to the background database, efficiency and so on. And now the popular PHP scripting language, its new features give us a new feeling. It supports design development in an object-oriented manner. At the same time, in order to meet the unique needs of Web pages, with templates, XML support and so on to bring a new method of Web site development.   In the language structure, PHP has a structure similar to the C + + language, and introduces the concept of classes, simplifying development. PHP also has a strong database support capability. In this case, we will first introduce the general process of PHP access to the database, and then through the file database storage to introduce PHP access to the database of an advanced application.   Finally, a practical and efficient database development method is introduced by using examples of database classes. Figure 1 PHP Database features PHP provides support for more than 10 common databases, such as Oracle, DBase, Informix, SQL Server, Sysbase, MySQL, and more.   It is because of extensive database support that it expands the scope of application of PHP so that various applications can be developed using PHP. In various databases, MySQL has been widely used because of its free, cross-platform, easy to use and high access efficiency.   Many central sites use Php+mysql as the best partner. Oracle is a typical large database application system.   Oracle is a good choice if you're designing a Web site with large data volumes, high performance and efficiency requirements. On the WIN32 platform, SQL Server occupies a large market.   PHP can access SQL Server.   PHP encapsulates the access methods of various databases, which are similar to the functions of different database systems, and increase the convenience of use. Below, we will be a simple talent information exchange Center (see Figure 1) as an example, the program to achieve the online resume of the submission, browsing and other functions, to tell the whole process of PHP database operation.   The database uses the most common MySQL database. Basic Steps for PHP database operation we will create a database named Resumedb on the local machine, which is known as the resume table in the database.   The table stores the number of resumes, the name of the person, the profile, and the CV file in Word format. 1. Create the database by switching to the/usr/local/mysql/bin directory, and at the command line, execute the following statement to create the database:./mysqlAdmin-u root-p Create resumedb Enter password: Enter the password after prompting.   If the database is used for the first time, the default password is empty and the direct carriage return is possible.   Then create a table that holds your CV.   Create a text file Resume.sql with the following: use Resumedb; CREATE TABLE Resume (ID tinyint (4) NOT NULL auto_increment, Name varchar (TEN) NOT NULL, Intro varchar (255), Re   Sufile Longblob, PRIMARY key (ID), key ID (ID)); Place it under my executable directory/usr/local/mysql/bin and execute the following command:./mysql-u root-p〈resume.sql Enter password: After entering the database password, the table Resume is automatically created successfully.   Among them, the Resufile field is the Longbolb type, used to store the binary Word document.   2. Database access process PHP access to the database typically includes the following steps: Connecting the database system → selecting a database → executing SQL statements → Closing the result set → closing the database connection → end. (1) Connecting the database connection database is the process of establishing the user program to the database system dialogue channel.   The statements that connect to the MySQL database are as follows: $LinkID = @mysql_connect ("localhost", "root", "") or Die ("Cannot connect to the database server!) The database server may not be started, or the user name password is incorrect!   "); ?) where the function mysql_connect () is used to establish a connection to the database server. The three parameters are: the host name of the database server (or IP), the database user name, and the user password.   The function return value is used to represent this database connection. As you can see from this command, we can specify that the machine name is not the local machine as the database server. In this way, it is possible to store the data offsite and secure the database isolation. Outside users often have the direct access to WWW server, if the database system is placed directly on the WWW server, it may bring security risks. If the database system is placed on a computer behind a firewall, PHP can access the database over the LAN, and the computer inside the LAN is invisible to the outside. In this way, the insuredThe possibility that the database is not subject to external attacks. The "@" symbol in front of the function to limit the display of error messages for this command. If there is an error in the function call, the statement after or is executed. The Die () function indicates that the program terminates execution after outputting the contents of the quotation marks to the user. This is done to prevent database connection errors when the user sees a bunch of confusing professional nouns, but instead prompts for a custom error message.   However, when debugging, we can still not block the error message, so that after the error, it is difficult to find out where there is a problem. (2) database Select a database system can contain multiple databases. After establishing a connection to the database server, we will tell the system which database we are going to use.   The command to select the database is as follows: @mysql_select_db ("Resumedb", $LinkID) or Die ("error selecting database, the database you specified may not exist!")   ");   ?) when you select a database, the parameters you provide are the name of the database, and the server connection number. When we use only one database server, $LinkID can omit, the system automatically finds a recent database connection and then uses it. However, when you want to implement a large site, it is necessary to encounter multi-host, multi-database system situation.   At this point, the database connection parameters cannot be omitted. (3) Database access OK, we have established a connection to the database, selected the database, the next thing is to execute the SQL statement.   The easy-to-use and powerful features of SQL statements will accomplish most of our database operations.   We can first write a personal information record to the database and then display it.   〈? $Name = "Openball";   In practice, $Name, $Intro is the value from the browser form $Intro = "Openball's profile ..."; $query = "INSERT into Resume (Name,intro) VALUES ($Name, $Intro)"; Generate SQL statement $result = @mysql_query ("$query", $LinkID); Execute if (! $result) echo "Data insertion failed!   "; $query = "Select Id,name,intro from Resume"; Generate SQL statement $result = mysql_query ($query, $LinkID); execution, the result set is saved to the variable $result $num = mysql_num_Rows ($result);    Gets the number of record rows returned by the query if ($num = = 0) {echo "did not find any records";   Exit (); } while ($row =mysql_fetch_array ($result))//Take the next row of data from the result set to the array $row {echo $row ["ID"]. " ". $row [" Name "]." ". $row [" Intro "]."   〈br〉 "; Access the value of the array variable with the field name index}? The above operation performed a total of two database operations. The first time is the insert operation, and the second is the query operation.   The program first inserts the current user's day record, and then displays the personal situation in all databases.   (4) The last resource release operation is to release the result set and release the result set and database connection resources.   〈?   @mysql_free_result ($result);   @mysql_close ($LinkID); ? If you have frequent database access across multiple Web pages, you can establish a continuous connection with the database server to improve efficiency.   Because each connection to the database server takes a long time and a large resource overhead, persistent connections are relatively more efficient. The method of establishing a persistent connection is to call the function mysql_pconnect () instead of mysql_connect () when the database is connected. The persistent connection established does not need to call Mysql_close () to close at the end of the program. The next time the program executes Mysql_pconnect (), the system automatically returns the ID number of the persistent connection that was established, instead of actually connecting to the database.

http://www.bkjia.com/PHPjc/532259.html www.bkjia.com true http://www.bkjia.com/PHPjc/532259.html techarticle (Author: Wang Kaibo) in the current rapid development of the Internet, e-commerce sites in endless situation, the Web site development efficiency and quality has put forward more and more high requirements. For large ...

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