Home > Backend Development > PHP Tutorial > How to transform the mysql_query statement to return a class object_PHP tutorial

How to transform the mysql_query statement to return a class object_PHP tutorial

WBOY
Release: 2016-07-13 17:23:32
Original
1118 people have browsed it

This is a way to simplify the SQL query function. I hope it will be helpful to you
If you have a better opinion, please OICQ 86804 or EMAIL: liboys@21cn.com to study together
# SQL query
# When When count=true, only count the number of records
function db_query($query, $rec = false) {

if($rec) {
$query = explode("from", $query ); // 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; // You can use the $re->xx method to call in the future
}

For example

...
# Just ask for the number of records
$query = "select id,name,about from table where id > 10";
$re = db_query($query,true); //Form like select count(*) from table where id > 10
print $re->reccount; //Return the number of records

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


$re=db_query($query);
for($i=0; $i reccount; $i++) {
$rows = mysql_fetch_object($re->result);
print $rows->id;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532216.htmlTechArticleThis is a way to simplify the SQL query function. I hope it will be helpful to you. If you have a better opinion, please OICQ 86804 or EMAIL: liboys@21cn.com Let’s study together # SQL query # When count=true,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template