Speed up the development of MySQL database program with Dbsql class

Source: Internet
Author: User
Tags include modify connect mysql php file table name mysql database
mysql| Program | data | database | speed

When you write access to MySQL database program, it is not very troublesome: a large set of functions and parameters, but also to check the results of the call, more headaches is every program to include database name, user, password, etc., want to modify are not easy. 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.

First, get Dbsql

How to get 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
-Download the Dbsql class directly from this station, I have made it independent, and make some minor changes. Download Address: http://www.phpuser.com/programs_and_code/codedetail.php?id=3

Second, modify the Dbsql file.
Open the file, find about 138 lines, and change the $host, $Database, $User, $Password, and other four variables to the values on your machine.

Iii. Use of Dbsql

It's so simple, it can come in handy, and here's a typical example (here we assume the Dbsql class is stored in the db.php file):
?
Require "db.php";
$db =new Dbsql;
$db->connect ();
if ($db->link_id)
{
$db->query ("Select ID, name from contacts WHERE ID > ID < 200");
if ($db->nf ())
{
while ($db->next_record ())
{
echo "id=", $db->f ("id");
echo "<br>";
echo "Name";
One $db->p (' name ');
echo "<br>";
}
}
$db->free_result ();
}
?>
Let me explain this step by line:
01-Include the db.php file in
02-Create an instance of the Dbsql class with the variable named: $db
03-Call the Dbsql Connect () method to connect to the database. This line of work is the same as Mysql_pconnect (host, DB, passwd)
04-Determine if the connection was successful by checking the value of the $db property link_id. In general, as long as the configuration is not a problem, this step can be omitted
05-If the connection is not a problem, call the Dbsql class's Query method to execute queries
The NF () function of the 06-dbsql class returns the number of bars of records returned after the query, as Mysql_num_rows (). If a record is found, continue execution
07-With a while loop, using the Dbsql Next_record () method as the condition. The Next_record () method moves the pointer of the result of the Dbsql class down one, and if it ends, 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 the method is the name of the field, such as $DB-&GT;F ("id")
11-Using the P () method of the Dbsql class. The P () method differs from the F () method by directly outputting the value of a field in the current row of the query result. The parameter of the method is the name of the field as well as the F () method, such as $DB-&GT;P ("id")
13-Frees up the memory that PHP consumes. Equivalent to calling the Mysql_free_result function

The basic usage of dbsql is this, and of course there are some other, I will introduce below.

Iv. Other Content

Auto_free property: If set to True value, when the Next_record () method is invoked to the end of the query result, Dbsql automatically executes the Free_result () method, releasing the memory consumed
DebugMode property: If set to True value, the query's SQL statement is printed when you execute the queries () method, so it is especially useful when debugging

Seek () method: Moves a pointer to a dbsql query result, the first is 0
Num_rows () Method: Like the NF () method, returns the number of record bars for the query result
Metadata () Method: Returns an array that includes the result of the table, using the table name as a parameter


Hope to bring a little benefit to everyone, but also welcome suggestions for improvement.



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.