简体   繁体   中英

php,mysql_query, mysql_fetch_array while loop of <a href>

I am kinda new to PHP and I am buliding a music library as a school project.

There is a Table 'albums' which holds 'id' and 'name'. and a table 'songs' containing 'id','name','album_id' and 'path'.

Long story short,I am trying to display all the songs that are in the selected album. user creates an album and then uploads songs into it.that part works great and the DB is filled in correctly. problem is, once I select an album to view the songs that are in it I get nothing.

        <?php
        $album_id = $_GET['id'];
        //display songs from selected album
        $query = mysql_query("SELECT * FROM songs WHERE album_id = $album_id");
        while ($fetch_songs = mysql_fetch_array($query)) {
            $song_name = $fetch_songs['name'];
            $song_path = $fetch_songs['path'];
            ?>
                    <a href="music/<?php echo $song_path ?>">play song</a>
                    <br/>
                    <b><?php echo $song_name; ?></b> 
            <?php
        }
        ?>

    </div>   

I believe using a href would be the simplest option, yet I've tried also audio controls and even trying to upload an image with img src istead of an MP3 and still no success, I just get an empty page.

this is the code for song uploading to DB.

            if (isset($_POST["upload"])) {
                $name = $_POST['name'];
                $album_id = $_POST['album'];

                $file_name = $_FILES['file']['name'];
                $file_type = $_FILES['file']['type'];
                $file_size = $_FILES['file']['size'];
                $file_tmp = $_FILES['file']['tmp_name'];
                $random_name = rand();


                if (empty($name) || empty($file_name)) {
                    echo "Please fill all fields in the form ! <br/>";
                } else {
                    move_uploaded_file($file_tmp, 'music/' . $random_name . '.mp3');
                    mysql_query("INSERT INTO songs VALUES ('','$name','$album_id','$random_name.mp3')");

                    echo "File uploaded ! </br></br>";
                }
            }
            ?>

Thanks for any help.

try this code :

while ($fetch_songs = mysql_fetch_array($query, MYSQL_ASSOC))

more information : http://php.net/manual/en/function.mysql-fetch-array.php

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