//$dbname string
//返回值 如果成功则返回 TRUE,失败则返回 FALSE。
function select_db ($dbname )
{
return $this->db->select_db($dbname);
}
//$query mysqli_result
//$resulttype int MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH. Defaults to MYSQLI_BOTH.
//返回值 Returns an array of strings that corresponds to the fetched row or NULL if there are no more rows in resultset.
function fetch_array(/*mysqli_result*/ $query, $resulttype = MYSQLI_ASSOC)
{
//var_dump(!null);
if(!$query || !($query instanceof mysqli_result))
return NULL;
return $query->fetch_array($resulttype);//
}
function data_seek($result,$offset)
{
return $result->data_seek($offset);
}
function fetch_assoc($query)
{
return $query->fetch_assoc();// 关联数组
}
function fetch_row($query)
{
return $query->fetch_row();// 索引数组,数字0,1。eg。。。
}
function fetch_fields($query)
{
return $query->fetch_fields();
}
//$query string
//$resultmode int
//返回值 如果成功则返回 TRUE,失败则返回 FALSE。 For SELECT, SHOW, DESCRIBE or EXPLAIN mysqli_query() will return a result object.
public function query($sql ,$resultmode=MYSQLI_STORE_RESULT )
{
if(MYSQL_OPEN_LOGS) {
$sqlstarttime = $sqlendttime = 0;
$mtime = explode(' ', microtime());
$sqlstarttime = $mtime[1]+ $mtime[0] ;
return $query;
}
//返回值 mysqli_stmt对象
function prepare($sql)
{
return $this->db->prepare($sql);
}
function affected_rows() {
return $this->db->affected_rows;
}
function error()
{
return $this->db->error;
}
function errno()
{
return $this->db->errno;
}
//result 没有
function num_rows($query)
{
return $query->num_rows;
}
//返回值 int The number of fields from a result set.
//也可以用另外一种方式 mysqliHelp->db->field_count返回。
function num_fields($query)
{
return $query->field_count;
}
function free_result($query)
{
//all methods are equivalent;
$query->free();
//$query->free_result();
//$query->close();
}
function insert_id()
{
if(($id = $this->db->insert_id)>= 0)
{
return $id;
}else
{
$idArr=$this->fetch_array($this->query("SELECT last_insert_id() as id"));
return intval($idArr[0]);
}
如果是MYSQLI_USE_RESULT,query以后得到mysqli_result对象后,执行data_seek会出错,因为
mysqli_result::data_seek() [mysqli-result.data-seek]: Function cannot be used with MYSQL_USE_RESULT
与MYSQLI_USE_RESULT相比,MYSQLI_STORE_RESULT 有着较高的内存和处理需求,因为是在客户机上维护整个结果集,所以内存分配和创建数据结构的耗费是非常巨大的,如果想一次检索多个行,可用 MYSQLI_USE_RESULT。
MYSQLI_USE_RESULT有着较低的内存需求,因为只需给每次处理的单行分配足够的空间。这样速度就较快,因为不必为结果集建立复杂的数据结构。
另一方面,MYSQLI_USE_RESULT把较大的负载加到了服务器上,它必须保留结果集中的行,直到客户机看起来适合检索所有的行。
*/
}
$dbHelper=new mysqliHelp;
$dbHelper->connect('localhost', 'root', '', 'tt');
//$dbHelper->db->select_db("tt");
//$dbHelper->db->set_charset("utf8");
//这里如果是MYSQLI_USE_RESULT,下面的$dbHelper->data_seek($query,10);就会出错
$query=$dbHelper->query("select id,cateid,title from product limit 22",MYSQLI_STORE_RESULT );
//$query=$dbHelper->query("update product set createtime=UNIX_TIMESTAMP() limit 22");
//$query=$dbHelper->query("insert into `product`(`cateid`,`title`,`text`,`createtime`) values (2,'test','content',1284822691)");
//$query=$dbHelper->query("delete from `product` where id=1");
//$query=$dbHelper->query("replace into product(id,cateid,title,text,createtime) values(1,2,'this is demo','test',UNIX_TIMESTAMP())");
//第1种
$sql="select id,cateid,title from product where cateid=? and title like ? and createtimeprepare($sql);
var_dump($stmt);
$stmt->bind_param('is',$cateid,$title);
$title="%%";
$cateid=10;
$stmt->execute();
$stmt->bind_result($col1, $col2,$col3);
//multi_query实例
//multi_query()方法的返回值,以及 //mysqli的属性errno、error、info等只与第一条SQL命令有关,无法判断第二条及以后的命令是否在执行时发生了错误。所以在执行 //multi_query()方法的返回值是TRUE时,并不意味着后续命令在执行时没有出错。
$sql="select id,cateid,title from product where cateid=4;";
$sql.="select id,cateid,title from product where cateid=10";
$query=$dbHelper->db->multi_query($sql);
if($query)
{
do {
/* store first result set */
//下面两种方法有什么区别?
// if ($result = $dbHelper->db->store_result()) {
// while ($row = $result->fetch_row()) {
// $data[]=$row;
// }
// $result->free();
// }
if ($result = $dbHelper->db->use_result()) {//返回mysqli_result类型
//$result->data_seek(0);//返回bool
while ($row = $result->fetch_row()) {
$data[]=$row;
}
//$result->close();
}