简体   繁体   中英

Trouble reading table in MySQL

I am trying to simply read a MySQL table names "songs" and write the "title" column into HTML. I am just beginning with MySQL, so can anyone explain why it is not working?

The (single-column) SQL table looks like this:

+---------+
|  TITLE  |
+---------+
|  Hello  |
|  World  |
|  Table  |
|  Value  |
+---------+

Here is the code I am using in the PHP page

<ul>
    <?php
        $dbc = mysqli_connect('localhost', 'root', 'pass', 'music');
        $query = "SELECT title FROM songs";
        $result = mysqli_query($dbc, $query);
        $row = mysqli_fetch_array($result);
        while ($row = mysqli_fetch_array($result)) {
            echo '<li id="' . $row['title'] . '" data-title="' . $row['title'] . '">';
            echo '<img class="X" src="X.png" style="width: 14px; margin: 2px 0 -2px -4px; display: none;" />';
            echo '<span class="title">' . $row['title'] . '</span>';
            echo '</li>';
        }
    ?>
</ul>

There is no output.

The first mysqli_fetch_array is not needed. You can delete this line:

$row = mysqli_fetch_array($result);

It should be working then.

<ul>
    <?php
        $dbc = mysqli_connect('localhost', 'root', 'pass', 'music');
        $query = "SELECT title FROM songs";
        $result = mysqli_query($dbc, $query);
        //$row = mysqli_fetch_array($result);
        while ($row = mysqli_fetch_row($result)) {
            echo '<li id="' . $row['title'] . '" data-title="' . $row['title'] . '">';
            echo '<img class="X" src="X.png" style="width: 14px; margin: 2px 0 -2px -4px; display: none;" />';
            echo '<span class="title">' . $row['title'] . '</span>';
            echo '</li>';
        }
    ?>
</ul>

Explanation:

mysqli_fetch_array() gives you the whole result set in an array(2-dimensional).

mysqli_fetch_row() gives you one entry from the result set, and moves the pointer onward. which is not good when you need to iterate over the results several times, but OK in your case!

You have a display: none on the element before, so maybe the output is there but hidden? check the "page source" from a browser just in case :)

Try this:

<ul>
<?php
    $dbc = mysqli_connect('localhost', 'root', 'pass', 'music');
    $query = "SELECT title FROM `songs`";
    $result = mysqli_query($dbc, $query);
    //$row = mysqli_fetch_array($result); You don't need this
    while ($row = mysqli_fetch_array($result)) {
        echo '<li id="' . $row['title'] . '" data-title="' . $row['title'] . '">';
        echo '<img class="X" src="X.png" style="width: 14px; margin: 2px 0 -2px -4px;     display: none;" />';
        echo '<span class="title">' . $row['title'] . '</span>';
        echo '</li>';
    }
?>
</ul>

Look at this line:

 $query = "SELECT title FROM `songs`";

Could you please eliminate the possibility of connection error:

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

And also, could you please encode results for displaying in html by replacing all $row['title'] to following:

htmlspecialchars($row['title'])

HTML part seems OK, so maybe you should try some debugging in code within while loop:

echo htmlspecialchars($row['title']); //only this within while loop

After that we can find out what is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM