Transforming the mysql_query statement how to return a class object _php tutorial

Source: Internet
Author: User
This is a way to simplify the SQL query function, and I hope to help you
If there is a better view, please OICQ 86804 or email:liboys@21cn.com co-study
# SQL query
# when Count=true, only the number of records is counted
function Db_query ($query, $rec = False) {

if ($rec) {
$query = Explode ("from", $query); A simple interception
$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; In the future, you can call $re->xx method.
}

For example
......
# just the number of records
$query = "Select Id,name,about from table where ID > 10";
$re = Db_query ($query, true); Shape as SELECT count (*) from table where ID > 10
Print $re->reccount; Returns the number of records

#求查询结果
$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;
}

http://www.bkjia.com/PHPjc/532216.html www.bkjia.com true http://www.bkjia.com/PHPjc/532216.html techarticle This is a way to simplify the SQL query function, I hope to help you if there is a better view, please OICQ 86804 or email:liboys@21cn.com co-study # SQL query # when count=true, ...

  • 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.