What should I do if I only want a key value but not a key name?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.