Heim  >  Artikel  >  Backend-Entwicklung  >  我这个只想要键值不想要键名 应该怎么弄?

我这个只想要键值不想要键名 应该怎么弄?

WBOY
WBOYOriginal
2016-06-20 12:41:22976Durchsuche

 $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;//将每次查的结果装到之前定义的数组
}



提示错误 是怎么回事?
 Fatal error: Call to a member function fetch_row() on a non-object in D:\wamp\www\cxblog2.php on line 9

$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;//将每次查的结果装到之前定义的数组
}



提示错误 是怎么回事?
 Fatal error: Call to a member function fetch_row() on a non-object in D:\wamp\www\cxblog2.php on line 9

你的sql查询语句是什么,可能你查出来的$result不是一个对象
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn