簡體   English   中英

為什么查詢數組會返回PHP中的對象數組?

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

我正在嘗試簡單地運行查詢並以數組形式獲取結果:

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;
}

在另一端:

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

結果:可捕獲的致命錯誤:類stdClass的對象無法轉換為字符串

我認為這是因為我使用了FETCH_OBJ ,但是我FETCH_OBJ什么來獲取字符串數組呢?

這是您可以執行的一種方法:

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);

嘗試print_R();

忍受

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

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM