简体   繁体   中英

value not displaying in mysql_fetch_array()

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("audio_book");
    $selectData = "SELECT * FROM user, library, audios 
                         WHERE user.user_id = library.user_id AND library.library_id = audios.library_id";
    $result = mysql_query($selectData) or die(mysql_error());
    //print_r(mysql_fetch_array($result)); // it works fine here
    while($row = mysql_fetch_array($result));
        echo $row['user_name']."-".$row['library_name']."-".$row['filename'];

as code given above m trying to get values from specified column names, issue is that it shows nothing on that place in echo while I get the result exactly as I wanted when used print_r(); what could be the issue?

Try this query instead. It makes use of the JOIN statement.

SELECT * FROM user INNER JOIN library ON user.user_id=library.user_id INNER JOIN audios ON library.library_id=audios.library_id

See the code below (Note I did make one change to your while statement)

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("audio_book");
    $selectData = "SELECT * FROM user INNER JOIN library ON user.user_id=library.user_id INNER JOIN audios ON library.library_id=audios.library_id";
    $result = mysql_query($selectData) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
        echo $row['user_name']."-".$row['library_name']."-".$row['filename'];
    }

I do recommend considering switching to MySQLi. I've laid out an example of how this can be done via mysqli below.

Procedural (since you are currently using procedural logic):

<?php
        $connect = mysqli_connect("localhost", "root", "password", "audio_book");
        $selectData = "SELECT * FROM user INNER JOIN library ON user.user_id=library.user_id INNER JOIN audios ON library.library_id=audios.library_id";
        $result = mysqli_query($connect,$selectData) or die(mysqli_error($connect));
        while($row = mysqli_fetch_array($result)){
            echo $row['user_name']."-".$row['library_name']."-".$row['filename'];
        }

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