Release the database result set. ClassDB {private $ host; service address private $ user; logon account private $ pass; logon password private $ name; database name private $ font; 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 );