What should I do if I only want a key value but not a key name? $ Pdo = new PDO ("mysql: host = localhost; dbname = t1", "root ","");
$…… = $ Pdo-> query ('select * from blog ');
$ Result = $ Something-> fetchALL (PDO: FETCH_ASSOC );
Foreach ($ result as $ v ){
Print_r ($ v );
}
?>
Reply to discussion (solution)
Use fetch_row () to re-create an array and load the retrieved content to the new array. in this way, no original key value name is available.
$ Rows = array (); // create an array to load the query results
While ($ row = $ result-> fetch_row () {// execute as long as the result can be found
$ Rows [] = $ row; // load the result of each query to the previously defined array
}
$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 '
';
Use fetch_row () to re-create an array and load the retrieved content to the new array. in this way, no original key value name is available.
$ Rows = array (); // create an array to load the query results
While ($ row = $ result-> fetch_row () {// execute as long as the result can be found
$ Rows [] = $ row; // load the result of each query to the previously defined array
}
What is the error message?
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 '';print_r($tmp_ary);echo '
';
The output is still an array.
$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 '
';
The key name is printed.
The parameter PDO: FETCH_ASSOC returns the join key array, with the key name column name.
The PDO: FETCH_NUM parameter returns the subscript array. the subscript starts from 0.
Fetch and fetchALL always return arrays, because php cannot predict the number of columns in the query results (syntax analysis is not performed on SQL commands)
Use fetch_row () to re-create an array and load the retrieved content to the new array. in this way, no original key value name is available.
$ Rows = array (); // create an array to load the query results
While ($ row = $ result-> fetch_row () {// execute as long as the result can be found
$ Rows [] = $ row; // load the result of each query to the previously defined array
}
What is the error message?
Fatal error: Call to a member function fetch_row () on a non-object in D: \ wamp \ www \ cxblog2.php on line 9
What is your SQL query statement? the $ result you may find is not an object.