Here is my somewhat broken strategy
-
If it is a query with no input ( always the same ), just use query();
$results = $this->database_top->query( $query ); -
If a single row is returned and there is input, do the prep (not shown here) and use
$results = $pdoStatement->fetch(PDO::FETCH_ASSOC); -
If multiple rows are returned and there is input,do the prep (not shown here) and use:
$results = $pdoStatement->fetchAll();
Problem I’m facing is that I need the first method to return an array or array of arrays like the second and third.
Prep looks like this FYI
$this->database_top->quote($query); // quote it
$pdoStatement = $this->database_top->prepare($query); // prepare it
$results = $pdoStatement->execute($parameterArray); // execute it
How can I modify my code so that all 3 methods return arrays or array of arrays?
Iterating over query()
$result_array = $this->DatabaseObject->_pdoQuery( 'multiple', 'tweet_model' );
foreach( $result_array as $array_1d )
{
$array_2d[]=$array_1d;
}
querydoes not return results, so I’m not sure what you meant on the first example.queryreturns a PDOStatement. It’s basically the same as:As vicTROLLA noted, the simplest solution is to just use:
However, if you need to process the results as you go for some reason, you could always loop over them manually.
$resultswill thus always be an array with 0 or more arrays inside of it.I find it useful to build arrays where the array key is the primary key of the record:
Also, I suspect that you’ve misunderstood the purpose and functionality of quote. It is used for escaping strings that you are going to interpolate into a query, not for magically escaping all values in a query (hence
$db->quote($query)makes no sense).Even worse is that drivers are not required to support quote. (Though it does at least return false if there’s no support.)
prepareis massively preferred overquote.