PDO fetchAll array to one dimensional
Take a look at example 2 in the manual. In your first query you can use:
$staff = $select->fetchAll(PDO::FETCH_COLUMN, 0);
And your second array will have the same form as the first array.
PDO fetch one column from table into 1-dimensional array
<?php
$sql = "SELECT `ingredient_name` FROM `ingredients`";
$ingredients = $pdo->query($sql)->fetchAll(PDO::FETCH_COLUMN);
PDO 'fetchAll' return rows with an extra array
fetchAll
will return a two-dimensional array.
- The first dimension is each row.
- The second dimension is each field (key/value pair) within that row.
$resultset[0]['fieldname']
will return the value of fieldname in the first row of the result set.
You're expecting fetchAll
to return a single row whereas that's what fetch
does.
So to keep it simple:
- If your result set will always only return one row, use
fetch
. - If your result set could return multiple (or zero) rows, use
fetchAll
and then for each to iterate through each row.
PDO retrieves two-dimensional array instead of one-dimensional array
PDO retrieves exactly what you ask it for.
To get a one-dimensional array you have to use PDO::FETCH_COLUMN
instead of PDO::FETCH_ASSOC
return $dbh->query("SELECT $column FROM $table" )->fetchAll(PDO::FETCH_COLUMN);
Note that your query is critically prone to SQL injection. PDO shouldn't be used that way. Do not try to automate SQL. It's already perfectly automated. Use a literal query like SELECT foo,bar FROM baz WHERE id=1
instead. There is not that much writing you're probably afraid of, and there will be not that much use of this function in a real world code as you probably hope for.
OR
use a ready-made query builder, such as Doctrine.
In PHP, is it possible to get a 1 dimmensional array using PDO?
Using the constant PDO::FETCH_COLUMN:
$columnNumber = 0;
$rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN, $columnNumber);
This way you'll get exactly what you proposed.
You can also do the following:
$columnNumber = 0;
$rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, $columnNumber);
This way you'll get an array with unique values.
Source: http://www.php.net/manual/en/pdostatement.fetchall.php
PHP PDO FetchAll - format of array
Although you are asking for PDO::FETCH_ASSOC
constant (by calling fetchAll(PDO::FETCH_ASSOC)
you'll get only single value), yet there is an even more intelligent fetch mode - PDO::FETCH_COLUMN
$ul1 = 0;
$sql = "SELECT word FROM tbl WHERE catid > ? ORDER BY RAND() LIMIT 3";
$stmt = $pdo->prepare($sql);
$stmt->execute([$ul1]);
$data = $stmt->fetchAll(PDO::FETCH_COLUMN);
var_dump($data);
this way you'll get just a simple one-dimensional array which you can address the most natural way:
$test = $data[2];
or iterate over with
foreach ($data as $word)
{
echo "$word</br>\n";
}
Create single array from MySQL with PHP/PDO
What you need is PDO::FETCH_COLUMN
mode:
$sql = "SELECT name FROM " . $this->table_name . " ORDER BY id";
$prep_state = $this->db_conn->prepare($sql);
$prep_state->execute();
// 0 indicates first column of result
$names = $prep_state->fetchAll(PDO::FETCH_COLUMN, 0);
print_r($names);
// then do what you want.
Related Topics
A Better Approach Than Storing MySQL Password in Plain Text in Config File
What Security Issues Should I Look Out for in PHP
How to Get Final Url After Following Http Redirections in Pure PHP
In PHP, How to Extract Multiple E-Mail Addresses from a Block of Text and Put Them into an Array
How to Use "Root" Namespace of PHP
Simple HTML Dom File_Get_HTML Not Working - Is There Any Workaround
How to Set a Default Attribute Value for a Laravel/Eloquent Model
$_Files Field 'Tmp_Name' Has No Value on .Jpg File Extension
How to Install Ziparchive on PHP 7.2 with Centos 7
Codeigniter Default Controller in a Sub Directory Not Working
Calling Function Inside Preg_Replace Thats Inside a Function
Decode a Quoted Printable Message in PHP
Programmatically Add Product to Cart with Price Change
How to Store an Array into MySQL
Inkscape Inside PHP/Apache Doesn't Render Fonts to Png
Concatenation with Addition in It Doesn't Work as Expected
Enabling Postgresql Support in PHP on MAC Os X
Function to Return Only Alpha-Numeric Characters from String