简体   繁体   English

为什么查询数组会返回PHP中的对象数组?

[英]Why does my query to array return an array of objects in PHP?

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 结果:可捕获的致命错误:类stdClass的对象无法转换为字符串

I assume this is because I use FETCH_OBJ , but what do I use to just get an array of strings? 我认为这是因为我使用了FETCH_OBJ ,但是我FETCH_OBJ什么来获取字符串数组呢?

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(); 尝试print_R();

to under stand the proplem 忍受

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

regard's 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM