mysqli_query fetch assoc 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: mysqli fetch assoc
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
$result->free();
}
$mysqli->close();
?>