SoFunction
Updated on 2025-03-09

Use DBSQL class to speed up the development of MySQL database programs

When you are writing a database program that accesses MYSQL, do you feel it is very troublesome: a large set of functions and parameters, and you also need to check the result of the call. What's even more troublesome is that each program must contain database name, user, password, etc., which is not easy to modify. But if you use the DBSQL class in PHPLIB, these problems will be 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 download a PHPLIB from this site or download a copy of PHPLIB
- Download the DBSQL class directly from this site, I have made it standalone and made some minor modifications. Download address: /programs_and_code/?id=3

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

3. Use DBSQL

It's that simple and can come in handy. Here is a typical example (here we assume that the DBSQL class is stored in a file):
<?  
01 require "";  
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");  
09 echo "<br>";  
10 echo "name";  
11 $db->p('name');  
12 echo "<br>";  
}  
}  
13 $db->free_result();  
}  
?>  
Let me explain it one by one:
01-Include the file
02-Create an instance of the DBSQL class with the variable name: $db
03-Call DBSQL's connect() method to connect to the database. The function of this line is as mysql_pconnect(host,
db, passwd) the same
04-Judge whether the connection is successful by checking the value of the property Link_ID of $db. Generally speaking, as long as the configuration is fine, this step can be omitted.
05-If the connection is fine, call the query method of the DBSQL class to execute the query.
06-The nf() function of the DBSQL class returns the number of records returned after the query, which is the same as the function of mysql_num_rows(). If the record is found, continue to execute
07-Use a while loop, with 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, and 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 parameters of this method are 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 outputs the value of a field in the current row of the query result directly. The parameters of this method are the same as the f() method, such as $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, and of course there are some others, which I will introduce below.

4. Other content

Auto_free attribute: If set to the true value, when the next_record() method is called to reach the end of the query result, DBSQL will automatically execute the free_result() method to free up the occupied memory. DebugMode attribute: If set to the true value, the query SQL statement will be printed out when executing the query() method, so it is particularly useful when debugging.

seek() method: move the pointer to the DBSQL query result, the first one is 0
num_rows() method: Like nf() method, it returns the number of records in the query result.
metadata() method: Take the table name as parameter, return an array containing the results of the table.