Home >Backend Development >PHP Tutorial >How to Separate MySQL Column Values Using mysqli_fetch_array?

How to Separate MySQL Column Values Using mysqli_fetch_array?

Susan Sarandon
Susan SarandonOriginal
2024-10-30 14:09:52365browse

How to Separate MySQL Column Values Using mysqli_fetch_array?

Unable to Separate MySQL Column Values with mysqli_fetch_array

In this scenario, you're encountering an issue where the mysqli_fetch_array function is combining all the column values into a single column, making it challenging to access them individually.

To resolve this, consider utilizing the following approach:

<code class="php">$posts = array();
while($row = mysqli_fetch_array($result)) {
    $posts[] = $row;
}</code>

By storing the entire row as an element in the $posts array, you can access individual values later using a nested loop:

<code class="php"><?php 
foreach ($posts as $row) 
{ 
    foreach ($row as $element)
    {
        echo $element . "<br>";
    }
}
?></code>

This will print all values in the columns, one per line. Alternatively, you can also access specific elements directly using the array index:

<code class="php">$postId = $row[0];</code>

The above is the detailed content of How to Separate MySQL Column Values Using mysqli_fetch_array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn