【问题标题】:PHP mysqli_fetch_all results nested in arraysPHP mysqli_fetch_all 结果嵌套在数组中
【发布时间】:2017-11-07 21:47:12
【问题描述】:

我正在尝试从数组中的某个数据库中获取所有表,以便将其与另一个数据库进行比较。

//Get a list of all tables
$sql = "SHOW TABLES FROM Data;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck == 0)
{
    exit("NO SQL RESULTS");
}

$array = mysqli_fetch_all($result);
print_r($array);

但是,当我这样做时,它会导致我得到一个数组,其中的结果嵌套在另一个数组中。

Array
(
    [0] => Array
        (
            [0] => table1
        )

    [1] => Array
        (
            [0] => table2
        )

    [2] => Array
        (
            [0] => table3
        )

    [3] => Array
        (
            [0] => table4
        )
)

有没有办法做到这一点,而不使用 while 循环与 mysqli_fetch_assoc 结合?

【问题讨论】:

    标签: php arrays mysqli


    【解决方案1】:

    你可以试试这样的:

    SELECT group_concat(table_name) as tables FROM information_schema.tables where table_schema='Data'
    

    这将返回一个字段tables,其中包含所有表名的逗号分隔列表。

    然后你可以使用:

    $array = mysqli_fetch_array($result);
    $tables = explode(",", $array['tables']);
    print_r($tables);
    

    将表名拆分为数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2013-06-17
      • 2014-10-20
      相关资源
      最近更新 更多