I'm using WordPress with XAMPP. I'm trying to fetch the post title from a column called post_title from a table called ltport_posts by using mysqli_fetch_row()
function. The connection with the database is working just fine. However, post titles don't seem to be written in the news ticker. Now I know that $row
variable is an enumerated array, so we should write the offset number to access the column. It should be noted that the while loop is working since I have four rows in the ltport_posts and four <div>
's are generated (browser's inspect element is showing me that). But the whole tag is empty:
<div class="ticker-wrap">
<div class="ticker">
<div class="ticker__item"> </div>
<div class="ticker__item"> </div>
<div class="ticker__item"> </div>
<div class="ticker__item"> </div>
</div>
</div>
Here's the php/HTML code:
<div class="ticker">
<?php $query="SELECT post_title from ltport_posts";
if($result=mysqli_query($conn,$query))
{
while($row=mysqli_fetch_row($result))
{?>
<div class="ticker__item"><?php printf("%s", $row[5]); ?> </div>
<?php }
mysqli_free_result($result);
}
mysqli_close($conn);?>
</div>
mysqli_result::fetch_row -- mysqli_fetch_row — Get a result row as an enumerated array
Your query is
$query="SELECT post_title from ltport_posts";
You just fetch one column
from your query. So you will get only $row[0]
.It's indexing start from 0
while($row=mysqli_fetch_row($result))
{?>
<div class="ticker__item"><?php printf("%s", $row[0]); ?> </div>
<?php }
For fetching multiple column
$query = "SELECT column1, column2,column3,column4 FROM City ORDER by ID DESC";
if ($result = mysqli_query($link, $query)) {
while ($row = mysqli_fetch_row($result)) {
printf ("%s (%s)\n", $row[0], $row[1],$row[3], $row[4]);
}
}