Answers for "how to print data from mysqli_fetch_assoc"

SQL
1

how to reverse fetch assoc in php

while($row = mysql_fetch_assoc($result)){
    $items[] = $row;
}

$items = array_reverse($items ,true);

foreach($items as $item){
   echo $item['time']." - ".$item['username']." - ".$item['message'];
}

//another option can be

$query = mysql_query("SELECT * FROM (
                      SELECT time, username, message
                      FROM messages ORDER BY time 
                      DESC LIMIT 10) result 
                      ORDER BY time ASC   
                    ");
Posted by: Guest on July-01-2020
3

mysqli fetch assoc

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %sn", $mysqli->connect_error);
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    $result->free();
}

/* close connection */
$mysqli->close();
?>
Posted by: Guest on March-07-2020

Code answers related to "how to print data from mysqli_fetch_assoc"

Code answers related to "SQL"

Browse Popular Code Answers by Language