$pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$sth=$pdo->query('select * from blog');
$result = $sth->fetchALL(PDO::FETCH_ASSOC);
foreach($result as $v){
print_r($v);
}
?>
用fetch_row(),重新建建一个数组,将查到的内容装到新数组里,这样就没原来的键值名了
$rows = array();//建立一个数组用来装查询结果
while($row = $result->fetch_row()){//只要能查到结果就执行
$rows[] = $row;//将每次查的结果装到之前定义的数组
}
$pdo=new PDO("mysql:host=localhost;dbname=mysql","root","");$sth=$pdo->query('select * from user');$result = $sth->fetchALL(PDO::FETCH_ASSOC);$tmp_ary=array();foreach($result as $v){ $tmp_ary[]=array_values($v);}echo '<pre class="brush:php;toolbar:false">';print_r($tmp_ary);echo '
用fetch_row(),重新建建一个数组,将查到的内容装到新数组里,这样就没原来的键值名了
$rows = array();//建立一个数组用来装查询结果
while($row = $result->fetch_row()){//只要能查到结果就执行
$rows[] = $row;//将每次查的结果装到之前定义的数组
}
$pdo=new PDO("mysql:host=localhost;dbname=mysql","root","");$sth=$pdo->query('select * from user');$result = $sth->fetchALL(PDO::FETCH_ASSOC);$tmp_ary=array();foreach($result as $v){ $tmp_ary[]=array_values($v);}echo '<pre class="brush:php;toolbar:false">';print_r($tmp_ary);echo '
$pdo=new PDO("mysql:host=localhost;dbname=mysql","root","");$sth=$pdo->query('select * from user');$result = $sth->fetchALL(PDO::FETCH_ASSOC);$tmp_ary=array();foreach($result as $v){ $tmp_ary[]=array_values($v);}echo '<pre class="brush:php;toolbar:false">';print_r($tmp_ary);echo '
参数 PDO::FETCH_ASSOC 返回关联键数组,键名为列名
参数 PDO::FETCH_NUM 返回下标数组,下标从 0 开始
fetch 和 fetchALL 总是返回数组,因为 php 不能预知查询结果有几列(不对 sql 指令做语法分析)
用fetch_row(),重新建建一个数组,将查到的内容装到新数组里,这样就没原来的键值名了
$rows = array();//建立一个数组用来装查询结果
while($row = $result->fetch_row()){//只要能查到结果就执行
$rows[] = $row;//将每次查的结果装到之前定义的数组
}