fetch assoc() in php code example
Example 1: mysqli_fetch_assoc
<?php
$con = mysqli_query("hostname", "username", "pwd", "database");
$sql = "SELECT * FROM `TABLE`";
$result = mysqli_query($con, $sql);
$check = mysqli_num_rows($result);
if($check > 0){
while($data= mysqli_fetch_assoc($result)){
echo $data["ColName"];
}
}
?>
Example 2: 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'];
}
$query = mysql_query("SELECT * FROM (
SELECT time, username, message
FROM messages ORDER BY time
DESC LIMIT 10) result
ORDER BY time ASC
");