Question

I'm trying to simply run a query and get the results in an array:

function run_query($query)
{
    $out = '';
    $db = new PDO("mysql:host=localhost;dbname=test","test","test");
    $out = $db->query($query)->fetchAll(PDO::FETCH_OBJ);
    return $out;
}

And on the other end:

$l_o_array = $php_functions->run_query('SHOW TABLES');
$temp = implode(',', $l_o_array);

Result: Catchable fatal error: Object of class stdClass could not be converted to string

I assume this is because I use FETCH_OBJ, but what do I use to just get an array of strings?

No correct solution

OTHER TIPS

Here's one way you might do it:

function run_query($query)
{
    $out = '';
    $db = new PDO("mysql:host=localhost;dbname=test","test","test");
    $out = $db->query($query)->fetchAll();
    return $out;
}

$results = run_query('SHOW TABLES');

$tables = array();
foreach($results as $result)
    $tables[] = $result['Tables in test']; // Note, replace "test" with your database name

$temp = implode(',', $tables);

try print_R();

to under stand the proplem

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>

regard's

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top