使用SQLI从SQL DB获取行并打印其数据

时间:2016-04-17 20:02:27

标签: php mysql mysqli

编辑:我调整了我的代码

$rows = 0;          
$connection = mysqli_connect("a", "a", "a", "a");
$rows = mysqli_query($connection, "SELECT COUNT(*) FROM chat");

for($i = 0; $i < $rows; $i++){
    $result = mysqli_query($connection, "SELECT * FROM chat WHERE chat_index = 1"); 
    $row = mysqli_fetch_all($result, MYSQLI_ASSOC);             

    echo "<span>".$row['name']."</span>";
    echo "<img src='".$row['avatar']."'></img>";
    echo "<span>".$row['flair']."</span>";
    echo "<span>".$row['message']."</span>";
}

然而没有任何表现。 https://CSGOVoid.net/chat

编辑:打印数据的最有效方法是什么?

enter image description here

应打印(按顺序):

-Name

-Avatar

-Flair

-Message

我试图遍历每一行并打印每列中的数据,我使用以下内容:

$connection = mysqli_connect("domain", "username", "password", "mytable");
$rows = mysqli_query($connection, "SELECT COUNT(*) FROM chat");

echo $rows;

for($i = 0; $i < $rows; $i++){
    $steamid = mysqli_query($connection, "SELECT steamid FROM chat WHERE chat_index = ".$i);
    $name = mysqli_query($connection, "SELECT name FROM chat WHERE chat_index = ".$i);
    $flair = mysqli_query($connection, "SELECT flair FROM chat WHERE chat_index = ".$i);
    $avatar = mysqli_query($connection, "SELECT avatar FROM chat WHERE chat_index = ".$i);
    $message = mysqli_query($connection, "SELECT message FROM chat WHERE chat_index = ".$i);

    echo "<span>".$name."</span>";
    echo "<img src='".$avatar."'></img>";
    echo "<span>".$flair."</span>";
    echo "<span>".$message."</span>";
}

我收到以下错误:

捕获致命错误:类mysqli_result的对象无法转换为字符串

1 个答案:

答案 0 :(得分:-1)

php文档:http://php.net/manual/en/mysqli.query.php

  

返回值:失败时返回FALSE。对于成功的SELECT,SHOW,   DESCRIBE或EXPLAIN查询mysqli_query()将返回一个mysqli_result   宾语。对于其他成功的查询,mysqli_query()将返回TRUE。

你之前的代码没有为$ steamid,$ name,$ flair等检索字符串,但实际上接收了对象,然后可以使用函数来获取你正在寻找的值:)。

如果有帮助,请告诉我