In doctrine DBAL2 when I execute a query like this:
<?php
$connection = $this->getDatabaseConnection();
$sql = "SELECT page_url
FROM cms_user_page
WHERE site_id = :siteid
AND active = '1'
";
$stmt = $connection->prepare($sql);
$stmt->bindValue("siteid", $id);
$stmt->execute();
return $stmt->fetchAll();
?>
I get a result like this:
Array
(
[0] => Array
(
[page_url] => index.php?action=login
)
[1] => Array
(
[page_url] => index.php?action=shoppingcart
)
[2] => Array
(
[page_url] => index.php?action=products
)
)
My question is, is there a fetch mode that produces an result like this:
Array
(
[0] => index.php?action=login
[1] => index.php?action=shoppingcart
[2] => index.php?action=products
)
I could not find any info about fetch modes in the documentation. and i could do an array map. But thats overhead in my opinion..
You can pass a fetch mode parameter to fetchAll().
$stmt->fetchAll(\PDO::FETCH_COLUMN)
This answer has been edited because this answer is correct.
You can use the FETCH_COLUMN fetch mode in fetchAll():
$stmt->fetchAll(\PDO::FETCH_COLUMN)
As another user points out in the comments, fetchAll() can return data formatted in a multitude of interesting formats.
Or you can iterate with fetchColumn():
while($page_url = $stmt->fetchColumn()) {
echo $page_url . PHP_EOL;
}
As of PHP5.5 you could use aray_column to achieve required result like so:
<?php
$connection = $this->getDatabaseConnection();
$sql = "SELECT page_url
FROM cms_user_page
WHERE site_id = :siteid
AND active = '1'
";
$stmt = $connection->prepare($sql);
$stmt->bindValue("siteid", $id);
$stmt->execute();
$data = array_column($stmt->fetchAll(), 'page_url');
return $data;
If you have more than one case where you need that form of result, although i dont really understand the sense too, you could implement AbstractHydrator interface to create your own ArrayHydrator that returns the structure as you need it.
Hydration Classes reside in NS:
Doctrine\ORM\Internal\Hydration
It looks like fetchAll was made deprecated in a recent version of DBAL. However I found another method called fetchFirstColumn which appears to do the same thing as fetchAll(\PDO::FETCH_COLUMN). I am currently on version 2.11.1 Doctrine DBAL.
I am using it like this in Symfony:
$statement = $connection->prepare('SELECT foo FROM bar WHERE baz = :baz');
$statement->execute([':baz' => 1]);
$result = $statement->fetchFirstColumn();
The value of $result will be a numerically indexed array starting at 0 like this:
[
0 => 'foo1',
1 => 'foo2'
];
As soon as you're requesting multiple rows in a database it does not make sense.
RDBMS stores rows and columns so the result is represented as rows and columns.
In the programming world it is called a matrix, in the PHP world it is an array.
________________
| id | name |
|______|_________|
| 1 | foo |
|______|_________|
| 2 | bar |
|______|_________|
will results in
array(
0 => array(
'id' => 1,
'name' => 'foo',
),
1 => array(
'id' => 2,
'name' => 'foo',
)
);
So no, you can't do that, you'd rather to process the result to fit your needs.
Related
I am using PHP PDO extension to create a list of all items from a DB category table.
The expected results are not correct, only one row of data returned instead of all
rows which is expected.
By running the same SQL statement within the phpMyAdmin console I get the expected
results for all rows of data.
I need to know what I am overlooking with pdo.
Current code:
$catId = 0;
$sql = "SELECT *
FROM category
WHERE cat_parent_id = :catId
ORDER BY cat_name";
$_stmt = $this->_dbConn->prepare($sql);
$_stmt->bindParam(":catId", $catId, PDO::PARAM_INT);
$_stmt->execute();
$rows = $_stmt->fetch(PDO::FETCH_ASSOC);
// display PDO result - only getting one row instead of all rows.
print_r($rows);
Array ( [cat_id] => 3
[cat_parent_id] => 0
[cat_name] => Shop
[cat_description] => Seminars
[cat_image] => c72e.gif
)
// NOTE: By running the same SQL within the phpMyAdmin
// console I get the expected results with all rows.
Array ( [cat_id] => 3
[cat_parent_id] => 0
[cat_name] => Shop
[cat_description] => Seminars
[cat_image] => c72e.gif
),
( [cat_id] => 1
[cat_parent_id] => 0
[cat_name] => Site Map
[cat_description] => List content links
[cat_image] => c83b.gif
)
PDOStatement::fetch() will only return 1 row at a time.
You could use PDOStatement::fetchAll():
$rows = $_stmt->fetchAll(PDO::FETCH_ASSOC);
or create a loop where you keep calling PDOStatement::fetch() until it returns false:
$rows = array();
while( $row = $_stmt->fetch(PDO::FETCH_ASSOC) ) {
$rows[] = $row;
}
But the latter example is a bit redundant, if you want to get all rows at once anyway, unless there are some memory concerns you want to address.
You have to use fetchAll like below:
$rows = $_stmt->fetchAll();
print_r($rows);
I have a Doctrine fetch statement like this
$query = "SELECT id FROM table LIMIT 2";
$result = $db->fetchAll($query);
which returns the array like this:
Array
(
[0] => Array
(
[id] => 1
)
[1] => Array
(
[id] => 2
)
)
Since the only column I fetch is ID, I don't need the array scope do be that deep. Is there a convenient way of making Doctrine return the results in a "flat" array, similar to what what PDO does:
$result = $db->query($query)->fetchAll(PDO::FETCH_COLUMN);
will return
Array
(
[0] => 1
[1] => 2
)
Currently I am flattening it using
$result = call_user_func_array('array_merge', array_map("array_values", $result));
You can simply use the PDO functionality (at least if you have MySQL).
$ids = $db
->executeQuery($query)
->fetchAll(\PDO::FETCH_COLUMN)
;
To resolve this problem you have to make your custom doctrine hydrator.
First: Make your own hydrator
<?php
namespace MyProject\Hydrators;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
class CustomHydrator extends AbstractHydrator
{
protected function _hydrateAll()
{
return $this->_stmt->fetchAll(PDO::FETCH_COLUMN);
}
}
Add your hydrator to Doctrine configuration file :
<?php
$em->getConfiguration()->addCustomHydrationMode('CustomHydrator','MyProject\Hydrators\CustomHydrator');
Finaly you can use your cutom hidrator :
<?php
$query = $em->createQuery('SELECT u FROM CmsUser u');
$results = $query->getResult('CustomHydrator');
I found a method called fetchFirstColumn which appears to do this. This was probably added in a later version of doctrine. I am currently on Doctrine ORM 2.7.4.
I am using it like this in Symfony:
$statement = $connection->prepare('SELECT foo FROM bar WHERE baz = :baz');
$statement->execute([':baz' => 1]);
$result = $statement->fetchFirstColumn();
The value of $result will be a numerically indexed array starting at 0 like this:
[
0 => 'foo1',
1 => 'foo2'
];
Fetch the data using fetchAssoc:
$result = $db->query($query)->fetchAssoc(PDO::FETCH_COLUMN);
You will get an Array like this:
Array (
[id] => 11
)
I have a simple PHP function that will grab information from a database based on a unique ID:
function oracleGetGata($query, $id="id") {
global $conn;
$results = array();
$sql = OCI_Parse($conn, $query);
OCI_Execute($sql);
while ( false!==($row=oci_fetch_assoc($sql)) ) {
$results[ $row[$id] ] = $row;
}
return $results;
}
So for example $array = oracleGetData('select * from table') would return:
[1] => Array
(
[Title] => Title 1
[Description] => Description 1
)
[2] => Array
(
[Title] => Title 2
[Description] => Description 2
)
This is fine, however, if I just want to return one record $array = oracleGetData('select * from table where id = 1') it returns the data like:
[] => Array
(
[Title] => Title 1
[Description] => Description 1
)
Not only am I unsure of how to reference this (there is nothing identifying the array) I'd also like to somehow change my function so if it just returns one record, it will just be a simple one dimensional array.
I've looked into the usual PHP array functions but can't find anything that'll help, nor an easy way of identifying how deep the array is.
Would really appreciate any help, thank you
Use array_pop():
$array = array_pop(oracleGetData('select * from table where id = 1'));
You will then get one single array:
Array
(
[Title] => Title 1
[Description] => Description 1
)
Instead of an array embedded in another one:
Array
(
[] => Array
(
[Title] => Title 1
[Description] => Description 1
)
}
I think there is an logic error in your script:
Change
$results[ $row[$id] ] = $row;
into
$results[] = $row;
The problem is, that you want to have the Database key value as array key value, but you don't know the Database key, since you don't know what the query will look like.
You could try:
$results[$row['id']] = $row;
But this only works when all of your results have a Database key named "id".
You pass a $id in the function signature, but in the loop you uses $row[$id], Why? Maybe the error is there.
If you want a sequencial id in your result set, you don't need use the $id, you can uses array_push() function
array_push($result, $row);
OR
$results[] = $row;
For some reason my array I am returning is not what I expect. Could someone explain to me why I am getting the current results, and what I can do to fix it? Here is the code in question:
public static function getProduct($_row, $_value)
{
$stmt = _DB::init()->prepare("SELECT pid, name, quantity, price, cost
FROM products
WHERE $_row = ?"
);
if($stmt->execute(array($_value)))
{
while ($row = $stmt->fetch())
return $row;
}
}
$product = Class::getProduct('pid',1);
print_r($product);
When I print the following array I am getting two results per row like so:
Array ( [pid] => 1 [0] => 1 [name] => Boondoggle [1] => Boondoggle [quantity] => 12 [2] => 12 [price] => 9.9900 [3] => 9.9900 [cost] => 12.9900 [4] => 12.9900 ) Boondoggle
I was only wanting to show the associative results. What is wrong with my function?
From the looks of it you are using PDO to communicate with your DBMS. The PDOStatement::fetch() method's first argument is a parameter to tell it what to return. By default it returns each column in both name format and numbered index format to allow iterating through columns easier. To just get the column names as indexes, you can pass it PDO::FETCH_ASSOC to your call. So the fetch statement would look like this:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)
See here for more details:
http://www.php.net/manual/en/pdostatement.fetch.php
Pass PDO::FETCH_ASSOC to your fetch call:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
edit: I'm just assuming you're using PDO, of course
I want to execute an SQL query like:
select 'tb1'.'f1','tb1'.'f2','tb2'.'f1' from 'tb1','tb2';
Now the problem is that i want to put it into an array in PHP like:
$result['tb1']['f1'], $result['tb1']['f2'], $result['tb2']['f1']...
Any idea how to achieve the above? Afaik there is no function which does the above. I was wondering the best simple way to do it. I do not want to use a query like "select .. as .. " unless necessary.
I do not know in advance what the fields will be, so I cannot assign them manually as suggest by the answer by benlumley.
Thank you,
Alec
You'll need to select the data as you are already doing, and then loop over it getting it into the format required, and because the fields have the same names, its easiest to use aliases or they'll just overwrite each other in the returned data (but you could use mysql_fetch_row instead, which returns a numerically indexed array).
For example:
$sql = "select tb1.f1 as tb1f1,tb1.f2 as tb1f2,tb2.f1 as tb2f1 from tb1,tb2";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$result['t1']['f1']=$row['tb1f1'];
$result['t1']['f2']=$row['tb1f2'];
$result['t2']['f1']=$row['tb2f1'];
}
(The quoting was wrong in your sql as well)
That won't handle multiple rows either, but your question sort of implies that you are only ever expecting one row?
WIthout aliases:
$sql = "select tb1.f1,tb1.f2,tb2.f1 from tb1,tb2";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
$result['t1']['f1']=$row[0];
$result['t1']['f2']=$row[1];
$result['t2']['f1']=$row[2];
}
I prefer the first version unless you have a good reason to use the second, as its less likely to result in errors if you ever change the sql or add fields etc.
EDIT:
Taking the meta data idea from the response below ....
<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname');
$result = mysql_query('select tb1.f1, tb1.f2, tb2.f1 from tb1, tb2');
$meta = array();
for ($i = 0; $i < mysql_num_fields($result); ++$i) {
$meta[$i] = mysql_fetch_field($result, $i);
}
while ($row = mysql_fetch_row($result)) {
foreach($row as $key=>$value) {
$out[$meta[$key]->table][$meta[$key]->name]=$value;
}
}
seems to do exactly what you are after - although you can still only get one row at a time.
Easily updated to store multiple rows with another dimension on the array:
Change:
$out[$meta[$key]->table][$meta[$key]->name]=$value;
To:
$out[][$meta[$key]->table][$meta[$key]->name]=$value;
Since you say you can't specify column aliases, and you can't know the fields of the query beforehand, I'd suggest a solution using mysql_fetch_field() to get metadata information:
<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname');
$result = mysql_query('select tb1.f1, tb1.f2, tb2.f1 from tb1, tb2');
for ($i = 0; $i < mysql_num_fields($result); ++$i) {
$meta = mysql_fetch_field($result, $i);
print_r($meta);
}
You can extract from this metadata information the table name and column name, even when there are multiple columns of the same name in the query.
PHP's ext/mysqli supports a similar function mysqli_stmt::result_metadata(), but you said you can't know the number of fields in the query beforehand, which makes it awkward to use mysqli_stmt::bind_result().
PDO_mysql doesn't seem to support result set metadata at this time.
The output from the above script is below.
stdClass Object
(
[name] => f1
[table] => tb1
[def] =>
[max_length] => 1
[not_null] => 0
[primary_key] => 0
[multiple_key] => 0
[unique_key] => 0
[numeric] => 1
[blob] => 0
[type] => int
[unsigned] => 0
[zerofill] => 0
)
stdClass Object
(
[name] => f2
[table] => tb1
[def] =>
[max_length] => 1
[not_null] => 0
[primary_key] => 0
[multiple_key] => 0
[unique_key] => 0
[numeric] => 1
[blob] => 0
[type] => int
[unsigned] => 0
[zerofill] => 0
)
stdClass Object
(
[name] => f1
[table] => tb2
[def] =>
[max_length] => 1
[not_null] => 0
[primary_key] => 0
[multiple_key] => 0
[unique_key] => 0
[numeric] => 1
[blob] => 0
[type] => int
[unsigned] => 0
[zerofill] => 0
)
Prefixing field names with short version of table name is a good way to achieve it without the need to create aliases in select. Of course you don't get the exact structure that you described but every field has its unique named index in result array. For example, let's assume you have table users, and the user has a name, then call this field usr_name.