0

EDIT: Ive adjusted my code

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

Yet nothing displays. https://CSGOVoid.net/chat


EDIT: What is the most efficient way of printing my data?

enter image description here

Should print (in order):

-Name

-Avatar

-Flair

-Message


I'm trying to go through every row and print the data in each column, I'm using the following:

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

I'm receiving the following error:

Catchable fatal error: Object of class mysqli_result could not be converted to string

1 Answers1

-1

php documentation: http://php.net/manual/en/mysqli.query.php

Return Values: Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

your previous code does not retrieve a string for $steamid, $name, $flair, etc, but actually receives objects, which then can use functions to obtain the values you are looking for :).

Let me know if that helped

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • So should I change my query to $rows = mysqli_query($connection, "SELECT COUNT(*) FROM chat").mysqli_fetch_all(); –  Apr 17 '16 at 20:11
  • try: `$result = mysqli_query($connection, "SELECT * FROM chat WHERE chat_index = 1"); $row = mysqli_fetch_all($result,MYSQLI_ASSOC); echo "steamid = ".$row['steamid'].", etc...
    ";` let me know if that works
    – Webeng Apr 17 '16 at 20:56
  • I made an edit just now, try it again with the new code and let me know – Webeng Apr 17 '16 at 21:00
  • Also I am assuming you are trying to make something like dota2lounge. I highly recommend PDO over mysqli. PDO is just a better (more object oriented) way of accessing a database, and most programmers agree that it's the direction MySQL is leaning towards, so it would be kind of... well annoying to have to change your code after a few months have passed because of the benefits you get from another method. – Webeng Apr 17 '16 at 21:03
  • Well, I suppose you could compare it to d2lounge, yes. WHats the advantages of mysql -> PDO? –  Apr 18 '16 at 15:44