How does the mysql_query statement return a class object?

Source: Internet
Author: User
This is a way to simplify the SQL query function, you want to help if you have a better view, please OICQ86804 or EMAIL: liboys@21cn.com Study # SQL query # When counttrue, calculate only the number of records functiondb_query ($ query, $ recfalse) {if ($ rec) {$ qu

This is a method to simplify SQL query functions.
If you have a better view, please study together with OICQ 86804 or EMAIL: liboys@21cn.com
# SQL query
# When count = true, only the number of records is calculated.
Function db_query ($ query, $ rec = false ){

If ($ rec ){
$ Query = explode ("from", $ query); // simple truncation
$ Query = "select count (*) from". $ query [1];
}

$ Result = mysql_query ($ query) or die (mysql_error ());

If ($ rec ){
$ Rows = mysql_fetch_row ($ result );
$ Reccount = $ rows [0];
}
Else {
$ Reccount = mysql_num_rows ($ result );
}

If ($ reccount ){
$ Re-> result = $ result;
$ Re-> reccount = $ reccount;
}
Else {
$ Re = false;
}
Return $ re; // you can call the $ re-> xx method later.
}

For example
......
# Count only Records
$ Query = "select id, name, about from table where id> 10 ";
$ Re = db_query ($ query, true); // select count (*) from table where id> 10
Print $ re-> reccount; // number of returned Records

# Query Results
$ Query = "select id, name, about from table where id> 10 ";


$ Re = db_query ($ query );
For ($ I = 0; $ I <$ re-> reccount; $ I ++ ){
$ Rows = mysql_fetch_object ($ re-> result );
Print $ rows-> id;
}

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.