Answer the question
In order to leave comments, you need to log in
Am I doing the SQL query correctly with mysqli ?
$mysqli = new mysqli('localhost', 'root', '', 'system');
function get_posts() {
global $mysqli;
$sql = "SELECT * FROM posts";
$result = $mysqli->query($sql);
$result = mysqli_fetch_assoc($result);
return $result;
}
$posts = get_posts();
foreach($posts as $post) {
echo '
<div class="post">
<div class="post-author"><a href="/profile.php?id='.$post['author-id'].'">'.$post['author-name'].' '.$post['author-surname'].'</a></div>
</div>
';
}
Answer the question
In order to leave comments, you need to log in
So mysqli_fetch_assoc() only fetches one row, not the entire selection! If you want to get the entire selection, you need to twist the mysqli_fetch_assoc call in a loop. Something like that
while ($row = $result->fetch_assoc()) {
....
}
And you extract one row, and then try to loop through it, thinking that you are looping through the records, but in fact, the loop is spinning through the fields of the first record.
Try to do $mysqli->set_charset('your character set');
There is an option that you do not specify the encoding in the Content-Type HTTP header
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question