【问题标题】:Simple Dump of Variable Contents变量内容的简单转储
【发布时间】:2013-07-25 00:34:08
【问题描述】:

我可以在 mySQL 数据库上成功执行查询,并使用以下行将输出分配给变量:

$result = mysqli_query($con,"select * from test");

如何在不使用数组和 while 循环的情况下转储变量 $result 的内容?

我尝试过: print_r($结果), 打印($结果), 打印$结果, 回显$结果

【问题讨论】:

  • ->fetch_assoc,如果有更多的行,你将需要一个循环。
  • without using an array and a while loop - 你为什么不想使用循环和数组?
  • 因为我发现使用数组和循环更加困难,此时我转储变量的内容以确认查询返回我期望的内容就足够了。
  • 只有 3 行,您可以直接复制粘贴我们的答案。

标签: php mysql variables


【解决方案1】:
$query= mysqli_query($con,"select * from test");
while($row = mysqli_fetch_assoc($query)){
  print_r($row);
}

没有测试,因为我不在电脑前,但我认为这是你需要的。

【讨论】:

    【解决方案2】:

    你可以使用循环和mysqli_fetch_assoc:

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        print_r($row);
    }
    

    如果你愿意,你可以把它变成一个函数,把它放在你的代码中,然后简单地调用它:

    function dumpQuery($result) {
        /* fetch associative array */
        while ($row = mysqli_fetch_assoc($result)) {
            print_r($row);
        }
    }
    
    ...
    
    $result = mysqli_query($con,"select * from test");
    dumpQuery($result);
    

    【讨论】:

    • 这个问题没有使用循环:)
    • 这是正确的@DevZer0“没有循环”。我想要的是快速而肮脏的第一次检查。我曾希望有一个函数(我还不知道)可以只转储变量的内容,如果这是正确的,那么我可以继续进行。我听到贡献者说的是没有这样的功能。
    • 除非你有mysqlnd for mysqli_fetch_all(),你可以为了方便定义一个函数并使用它。
    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2022-11-28
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    相关资源
    最近更新 更多