简体   繁体   English

如何在 php 中将 sql 查询的结果输出到屏幕上

[英]How do I output the result from an sql query onto the screen in php

The following is the code I use to try and achieve this.以下是我用来尝试实现这一目标的代码。

$con=mysqli_connect("localhost:8889","root","root","booksmart_properties");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    else{

        echo "we connected";
    }

    // Perform queries 
    $sql=mysqli_query($con,"SELECT * FROM ListedProperties");
    $result=mysqli_fetch_assoc($sql);



    echo $result['*'];


    mysqli_close($con);

?>

I'm new to php and i'm sure it's something small, I just can't see it.我是 php 的新手,我确定它很小,我只是看不到它。

Use PHP foreach loop like this:像这样使用 PHP foreach 循环:

foreach($result as $key => $val)
{
     print $val;
}

You are using您正在使用

$result = mysqli_fetch_assoc($sql);

which will fetch a result row as an associative array它将获取结果行作为associative array

So you can call your result with a key to get a value.所以你可以用一个键调用你的结果来获取一个值。

example:例子:

$result['id']

or to get all:或获得所有:

foreach($result as $key => $value)
{
    print $value;
}

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

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