mysqli循环数据

mysqli:循环数据显示到页面

   连接数据库smarty_fenye 数据表tbl_product

mysqli-----连接对象

mysqli_result----select结果集对象

mysqli_stmt----预处理对象

 

第一步:连接数据库mysqli

   $mysqli=new mysqli(“localhost”,”root”,”123”,”smarty_fenye”);

第二步:发送多条语句

   $sql=”set names gbk;select * from tbl_product;select name price tbl_product”;

第三步:循环显示

if($mysqli->multi_query($sql)){

      do{

             if($result=$mysqli->store_result()){

                     while($row=$result->fetch_row()){

                           foreach($row as $value){

                                

                                echo $value."<br>";

                                       }

                                   }

                      echo "<br>";

                      //返回总行数

                     echo "总行数:".$result->num_rows;

                     

                     echo "<br>";

                     echo "总列数:".$result->field_count;

                     

                     echo "<br>";

                     echo $result->current_field."当前列";

                     

                     echo "<br>";

                     @ $result->field_seek(1);

                     echo $result->current_field."当前列";

                     

                     echo "<br>";

                     $a=$result->fetch_field();

                     echo "名称1:".$a->name;

                     

                     echo "<br>";

                     @ $result->field_seek(2);

                     echo $result->current_field."当前列";

                     

                     echo "<br>";

                     $b=$result->fetch_field();

                     echo "名称2:".$b->name;

                     echo "<br>";

                  }

            

          }while($mysqli->next_result());

     }

 

还可以用:$mysqli->fetch_row():

  $sql="set names gbk;select * from tbl_product;select price from tbl_product";

if($mysqli->multi_query($sql)){

    do{

    if($result=$mysqli->store_result()){

  while($row=$result->fetch_assoc()){

  echo "名字是".$row['name'];

  echo "价格是".$row['price'];

  echo "描述是".$row['description']."<br>";

   }

  }

 }while($mysqli->next_result());

}

 

知识点:

  ① $affected_rows在前一个MYSQL操作中获取影响的行数

例如:$mysqli->query("insert into tbl_product(name,price,description) values('name5',1.05,'text3')");

     echo $mysqli->$affected_rows;

 

  ② $insert_id返回使用最后查询自动生成的编号

     例如:echo $mysqli->$insert_id;

 

  ③$num_rows返回结果集中包含记录的行数

例如:echo $result->$num_rows;

 

  ④get_client_info()返回客户端版本

例如:echo $mysqli->get_client_info();

 

  ⑤get_server_info()返回MYSQL服务器的版本

例如:echo $mysqli->get_server_info();

 

  ⑥more_results()从多查询语句中检查是否有任何更多的查询结果

例如:echo $mysqli->more_results();

 

  ⑦next_result()从当前执行的多查询中读取下一个结果多用于while($result->$mysqi->fetch_row())中

例如:echo $mysqli->next_result();

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值