vcapra1
vcapra1

Reputation: 2025

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.

Upvotes: 1

Views: 74

Answers (5)

Malkocoglu
Malkocoglu

Reputation: 477

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.

Upvotes: 0

Manolo
Manolo

Reputation: 26370

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`";

Upvotes: 0

ROunofF
ROunofF

Reputation: 730

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 :)

Upvotes: 0

Oleg Belousov
Oleg Belousov

Reputation: 10121

<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!

Upvotes: 0

Patrick
Patrick

Reputation: 131

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

$row = mysqli_fetch_array($result);

It should be working then.

Upvotes: 2

Related Questions