Questions about releasing database result sets

Source: Internet
Author: User
Release the database result set. ClassDB {private $ host; // service address private $ user; // logon account private $ pass; // logon password private $ name; // questions about releasing the database result set.
Class DB {
Private $ host; // service address
Private $ user; // logon account
Private $ pass; // logon password
Private $ name; // database name
Private $ font; // database character set
Private $ conn; // database connection source
Private $ result; // result set
Private $ rowRst; // query a single record
Private $ rowArray = array (); // query multiple records
Private $ rowAff; // add, modify, and delete records

Public function _ construct ($ host, $ user, $ pass, $ name, $ font ){
$ This-> host = $ host;
$ This-> user = $ user;
$ This-> pass = $ pass;
$ This-> name = $ name;
$ This-> font = $ font;
$ This-> init_conn ();
}

// Connect to the database
Private function init_conn (){
If (! $ This-> conn = mysql_connect ($ this-> host, $ this-> user, $ this-> pass )){
Exit ('database connection error ');
}
If (! Mysql_select_db ($ this-> name, $ this-> conn )){
Exit ('database selection error ');
}
If (! Mysql_query ('set names'. $ this-> font )){
Exit ('character set error ');
}
}

// Execute the SQL statement
Public function _ query ($ SQL ){
If (! $ This-> result = mysql_query ($ SQL, $ this-> conn )){
Exit ('SQL execution failed ');
}
Return $ this-> result;
}

// Query Singleton data
Public function _ fetch_array ($ SQL ){
$ This-> _ query ($ SQL );
$ This-> rowRst = mysql_fetch_array ($ this-> result, MYSQL_ASSOC );
Return $ this-> rowRst;
}

// Query multiple data entries
Public function _ fetch_array_list ($ SQL ){
$ This-> _ query ($ SQL );
While (!! $ Rows = mysql_fetch_array ($ this-> result, MYSQL_ASSOC )){
$ This-> rowArray [] = $ rows;
}
Return $ this-> rowArray;
}

// Add, modify, and delete the number of affected records
Public function _ affected_rows (){
$ This-> rowAff = mysql_affected_rows ();
Return $ this-> rowAff;
}

// Obtain the ID number of the previous INSERT operation
Public function _ insert_id (){
Return mysql_insert_id ();
}

// Release the result set
Public function _ free_result (){
$ This-> rowAff = '';
$ This-> rowArray = '';
$ This-> rowRst = '';
Mysql_free_result ($ this-> result );
}

// Close the database
Public function _ close_conn (){
$ This-> _ free_result ();
Mysql_close ($ this-> conn );
}
}
When I use the _ close_conn method, mysql_free_result (): supplied argument is not a valid mysql result resource is displayed on the webpage.
How to solve this problem


------ Solution --------------------
Before mysql_free_result ($ this-> result); you should first take a look at what $ this-> result has become. Is it the resource you opened?
------ Solution --------------------
If (is_resource ($ this-> result ))
{
Mysql_free_result ($ this-> result );
$ This-> result = NULL;
}
------ Solution --------------------
The value of your $ this-> result is as follows:
$ This-> result = mysql_query ($ SQL, $ this-> conn)

Mysql_query only returns resources when the select statement is executed, and others are logical values.

So mysql_free_result ($ this-> result );
To write
If (is_resource ($ this-> result) mysql_free_result ($ this-> result );

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.