0

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.

4
  • 1
    Add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); see if it yields anything, including or die(mysql_error()) to mysql_query(). Commented Sep 7, 2014 at 15:16
  • nope,not getting any error report. Commented Sep 7, 2014 at 15:28
  • is albumid is not integer you need to enclose it in quotes Commented Sep 7, 2014 at 15:36
  • You mean like this? : ("SELECT * FROM songs WHERE 'album_id = $album_id'") Doesn't seems to make any difference. Commented Sep 7, 2014 at 15:47

1 Answer 1

0

try this code :

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

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

5
  • Still getting an empty album :( Commented Sep 7, 2014 at 15:30
  • echo $song_name and $song_path in while and copy the result here. Commented Sep 7, 2014 at 15:52
  • Mmm,that's weird. still nothing. so the problem is with the SQL query? cause its doesn't seems to read the data from the DB. Commented Sep 7, 2014 at 16:07
  • use "SELECT * FROM songs WHERE album_id = 1" ,at MYSQL(SQL tab) to check your query. Commented Sep 7, 2014 at 16:15
  • But 'album_id' is a varchar(yeah,I know,that's kinda stupid) its the name of the album. that's why there is the $album_id variable. even if I'm trying : ("SELECT 'name','path' FROM songs WHERE 'album_id = Superunknown'") To get that specific album I'm still getting nothing. Commented Sep 7, 2014 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.