Using DBSQL to speed up MySQL database development

Source: Internet
Author: User
Using the DBSQL class to speed up MySQL database program development. when you write a database program that accesses MYSQL, do you feel very troublesome: a large set of functions and parameters, and check the call results, the biggest headache is that every program should contain the database name, user, password, and so on, and it is not easy to modify it. But if you use the DBSQL class in PHPLIB, these problems will be solved. This article will teach you how to use the DBSQL class.


1. obtain DBSQL

There are two ways to obtain DBSQL:
-Since DBSQL is part of PHPLIB, you can download a PHPLIB copy from this site or http://phplib.netuse.de
-Download the DBSQL class directly from this site. I have made some minor changes and made it independent. : Http://www.phpuser.com/programs_and_code/codedetail.php? Id = 3

2. modify the DBSQL file.
Open the file, find around 138 rows, and change four variables, including $ Host, $ Database, $ User, and $ Password, to the values on your machine.

III. use DBSQL

This is simple and can be used. The following is a typical example (Here we assume that the DBSQL class is stored in the db. php file ):
01 require "db. php ";
02 $ db = new DBSQL;
03 $ db-> connect ();
04 if ($ db-> Link_ID)
{
05 $ db-> query ("SELECT id, name FROM contact WHERE id> 100 AND id
<200 ");
06 if ($ db-> nf ())
{
07 while ($ db-> next_record ())
{
08 echo "id =", $ db-> f ("id ");
09 echo"
";
10 echo "name ";
11 $ db-> p ('name ');
12 echo"
";
}
}
13 $ db-> free_result ();
}
?>
Let me explain it one by one:
01-include the db. php file
02-create a DBSQL instance with the variable name: $ db
03-call the connect () method of DBSQL to connect to the database. The role of this line is mysql_pconnect (host,
Db, passwd) Same
04-check the value of the $ db attribute Link_ID to determine whether the connection is successful. This step can be omitted as long as the configuration is correct.
05-if the connection is correct, call the query method of the DBSQL class to execute the query.
The nf () function of the 06-DBSQL class returns the number of records returned after the query, which is the same as that of mysql_num_rows. If a record is found, continue to execute
07-use a while loop and use the next_record () method of DBSQL as the condition. The next_record () method moves the result pointer of the DBSQL class down one by one. if it reaches the end, it returns a false value.
08-use the f () method of the DBSQL class to retrieve the value of a field in the current row of the query result. The parameter of this method is the name of the field, for example, $ db-> f ("id ")
11-use the p () method of the DBSQL class. The p () method differs from the f () method in that it outputs the value of a field in the current row of the query result directly. The parameter of this method is the same as that of the f () method and also the field name, for example, $ db-> p ("id ")
13-release the memory occupied by PHP. It is equivalent to calling the mysql_free_result function.

This is the basic usage of DBSQL. of course, there are other things that I will introduce below.

4. Other content

Auto_free attribute: if it is set to true, when the next_record () method is called to reach the end of the query result, DBSQL automatically executes the free_result () method to release the occupied memory DebugMode attribute: if it is set to the true value, the queried SQL statement is printed when the query () method is executed, so it is particularly useful for debugging.

Seek () method: move the pointer of the DBSQL query result. The first one is 0.
Num_rows () method: same as the nf () method, return the number of records in the query result.
Metadata () method: returns an array containing the results of a table named as a parameter.

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.