Home > Backend Development > PHP Tutorial > 我这个只想要键值不想要键名 应该怎么弄?

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

WBOY
Release: 2016-06-20 12:41:22
Original
1026 people have browsed it

 $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 '
Copy after login
Copy after login
Copy after login
';

用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 '
Copy after login
Copy after login
Copy after login
';


这打印出来的还是一个数组啊

$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 '
Copy after login
Copy after login
Copy after login
';


这样打印出来还是有键名啊

参数 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不是一个对象
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