一開始這樣寫的
$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);
}
?>
後來人家告訴我這樣寫 可是這樣列印出來的還是和原來一樣 那應該怎麼改呢
$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 '
';
print_r($tmp_ary);
echo '
';
回複內容:
一開始這樣寫的
$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);
}
?>
後來人家告訴我這樣寫 可是這樣列印出來的還是和原來一樣 那應該怎麼改呢
$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 '
';
print_r($tmp_ary);
echo '
';
array_values
瞭解PHP去除鍵的函數。
"li", "sex" => 1 ), array( "user" => "er", "sex" => 2 ) ); var_dump($result); $new = array(); foreach($result as $row){ $new[] = array_values($row); } var_dump($new);
在mysql取出資料的時候,貌似PDO 好像都帶了欄位的資訊,你用的時候注意下