0
<?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'];

正如上面给出的代码 m 试图从指定的列名中获取值,问题是它在 echo 中的那个地方没有显示任何内容,而我在使用 print_r() 时得到的结果完全符合我的要求;可能是什么问题?

4

1 回答 1

0

试试这个查询。它使用 JOIN 语句。

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

请参阅下面的代码(注意我确实对您的 while 语句进行了更改)

<?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'];
    }

我确实建议考虑切换到 MySQLi。我已经列出了如何通过下面的 mysqli 完成此操作的示例。

程序(因为您当前正在使用程序逻辑):

<?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'];
        }
于 2012-07-14T15:56:36.783 回答