Home > Backend Development > PHP Tutorial > Use the DBSQL class to speed up the development of MySQL database programs_PHP Tutorial

Use the DBSQL class to speed up the development of MySQL database programs_PHP Tutorial

WBOY
Release: 2016-07-21 16:02:42
Original
802 people have browsed it

When you write a database program to access MYSQL, do you find it very troublesome: a large set of functions and parameters, and the results of the calls must be checked. What's even more troublesome is that each program must include the database name, user, and password. Wait, it’s not easy to modify it. But if you use the DBSQL class in PHPLIB, these problems will be easily solved. This article will teach you how to use DBSQL classes.


1. Obtain DBSQL

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

2. Modify the DBSQL file.
Open the file, find about line 138, and change the four variables $Host, $Database, $User, $Password, etc. to the values ​​on your machine.

3. Using DBSQL

It’s that simple and can come in handy. 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 line by line:
01-Include the db.php file Come in
02-Create an instance of the DBSQL class with a variable name: $db
03-Call the connect() method of DBSQL to connect to the database. The function of this line is the same as mysql_pconnect(host,
db, passwd)
04- Determine whether the connection is successful by checking the value of the attribute Link_ID of $db. Generally speaking, as long as there is no problem with the configuration, this step can be omitted.
05-If there is no problem with the connection, call the query method of the DBSQL class to execute the query.
06-The nf() function of the DBSQL class returns the query. The number of records has the same function as mysql_num_rows(). If a record is found, continue execution
07-Use a while loop, conditional on the next_record() method of DBSQL. The next_record() method moves the pointer of the result of the DBSQL class down one line. 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, such as $db->f("id")
11-Use the p() method of the DBSQL class. The difference between the p() method and the f() method is that it directly outputs the value of a field in the current row of the query result. The parameter of this method is the same as the f() method, which is also the name of the field, such as $db->p("id")
13- Release the memory occupied by PHP. It is equivalent to calling the mysql_free_result function

The basic usage of DBSQL is this. Of course, there are others, which I will introduce below.

4. Other content

Auto_free attribute: If 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 set to true, the SQL statement of the query will be printed out when the query() method is executed, so it is particularly useful when debugging.

seek() method: Move the pointer of the DBSQL query result , the first one is 0
num_rows() method: same as nf() method, returns the number of records of the query result
metadata() method: takes the table name as parameter, returns a result including the table Array

http://www.bkjia.com/PHPjc/316498.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316498.htmlTechArticleWhen you are writing a database program to access MYSQL, do you find it very troublesome: a large set of functions and parameters, and also check the result of the call. What’s even more troublesome is that each program must contain data...
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