PHP Classes

"query" function return in Array not object

Recommend this page to a friend!

      PDO Multi Connection Class  >  All threads  >  "query" function return in Array not...  >  (Un) Subscribe thread alerts  
Subject:"query" function return in Array not...
Summary:query function return
Messages:7
Author:Marco Rossi
Date:2012-09-25 14:22:23
Update:2012-10-03 09:10:37
 

  1. "query" function return in Array not...   Reply   Report abuse  
Picture of Marco Rossi Marco Rossi - 2012-09-25 14:22:23
Hi!!
i've mod the line 219 in:
[PHP]
return $obj->fetchAll(PDO::FETCH_ASSOC);
[/PHP]

And now the "query_secure" function returns not an object, but an array of elements.

How can i make the same thing in the "query" function?

Thank you!!

  2. Re: "query" function return in Array not...   Reply   Report abuse  
Picture of Evert Ulises German Soto Evert Ulises German Soto - 2012-09-25 22:15:40 - In reply to message 1 from Marco Rossi
Hi Marco...

Before you execute any loop for get the recordset
you must specify that you need objects and not arrays (fetch assoc)

Example:
$rs = $db->query("SELECT mycolumn FROM mytable;");
$rs->setFetchMode(PDO::FETCH_OBJ);
foreach($rs as $row){
echo "Column value: " . $row->mycolumn . "<br/>";
}
$rs = null;


Regards!

  3. Re: "query" function return in Array not...   Reply   Report abuse  
Picture of Marco Rossi Marco Rossi - 2012-09-26 07:33:15 - In reply to message 2 from Evert Ulises German Soto
Thank you for the fast reply!!!

The script by default returns an OBJECT, so my question is: how to receive an array of elements?

I changed your example in:

$rs = $db->query("SELECT mycolumn FROM mytable;");
$rs->setFetchMode(PDO::FETCH_ASSOC);

But if i make a print_r($rs) i have:

" PDOStatement Object ( [queryString] => SELECT mycolumn FROM mytable )"

i would want to receive the array :D

Thank you very much!!

Regards!

  4. Re: "query" function return in Array not...   Reply   Report abuse  
Picture of Marco Rossi Marco Rossi - 2012-09-26 07:43:38 - In reply to message 2 from Evert Ulises German Soto
So when i make the print_r($rs) of $rs i would want to receive an array like this:

Array ( [0] => Array ( [Contenuto] => Contenuto_0 ) [1] => Array ( [Contenuto] => Contenuto_1 ) [2] => Array ( [Contenuto] => Contenuto_2 ))



When i made that change in query_secure function, works well, in the query function not.. Where i do wrong?

Thank you very much!

  5. Re: "query" function return in Array not...   Reply   Report abuse  
Picture of Marco Rossi Marco Rossi - 2012-10-02 09:19:24 - In reply to message 2 from Evert Ulises German Soto
Any news about this problem?

  6. Re: "query" function return in Array not...   Reply   Report abuse  
Picture of Evert Ulises German Soto Evert Ulises German Soto - 2012-10-02 19:40:52 - In reply to message 5 from Marco Rossi
Hi Marco... I've been trying but the results are the same.

The only way for you get an Array or Object is apply the change to fetch mod:
if you want a objects:
$rs->setFetchMode(PDO::FETCH_OBJ);
and $rs->setFetchMode(PDO::FETCH_ASSOC);
for arrays...

here is the partial solution:

foreach($rs as $row){
var_dump($row);
echo "<br/>";
}

The result is something like this:
Objects:
object(stdClass)[4]
public 'intranet_administracion_usuarios_id' => string '13338' (length=5)
public 'intranet_administracion_usuarios_numemp' => string '94164053' (length=8)

Arrays:
array
'intranet_administracion_usuarios_id' => string '13338' (length=5)
0 => string '13338' (length=5)
'intranet_administracion_usuarios_numemp' => string '94164053' (length=8)
1 => string '94164053' (length=8)

I hope this has been helpful...

  7. Re: "query" function return in Array not...   Reply   Report abuse  
Picture of Marco Rossi Marco Rossi - 2012-10-03 09:10:37 - In reply to message 6 from Evert Ulises German Soto
Thank you very much!

Have a nice day!