Home > Article > Backend Development > vb connects to sql database using DBSQL class to speed up the development of MySQL database program
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 download a copy of PHPLIB from this site or http://phplib.netuse.de
- Directly from this site Download the DBSQL class, I've made it standalone 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 such as $Host, $Database, $User, and $Password to the values on your machine.
3. Using 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 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 "< br>";
}
}
13 $db->free_result();
}
?>
Let me explain it line by line:
01-Include the db.php file
02-Create a DBSQL class Instance, 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 number of records returned after the query. , 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, after When the query() method is executed, the SQL statement of the query will be printed out, 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: followed by nf( ) method, returns the number of records of the query result
metadata() method: takes the table name as a parameter and returns an array including the results of the table
The above introduces the use of VB to connect SQL database to speed up the development of MySQL database programs using the DBSQL class, including the content of VB connection to SQL database. I hope it will be helpful to friends who are interested in PHP tutorials.