-1

can somebody please tell me what's wrong with my code ?! i stored the file path in my database as varchar and it's 01.mp3 and the file is right next to the php file but it's not working and i'm using Mysql here's the function file getAudio.php

<?php
include 'conn.php';
// to connect with the database
  $id = $_GET['id'];
  // validation just to be safe

  $sql = "SELECT file FROM filepath WHERE id=$id"; 
  // choose the column from the table where the id= the id of the audio file

  $result = mysql_query("$sql");
  //query the audio file

  $row =  mysql_fetch_assoc($result);
  //Returns an associative array

  mysql_close($link);
  //closes the non-persistent connection to the MySQL



 echo $row['file'];
 //calls the audio file and returns it in the src attribute
?>

and here's where i'm trying to echo it song.php

<?php
include 'conn.php'; ?> 

<html> 
<head><title></title></head>
<body>


<audio controls> 
<source src="<?php echo '/getAudio.php?id=1/';?>" type="audio/mpeg"> 


</audio>




</body>
</html>

and when i replace the '' with "" the audio appears but it doesn't play

  • You probably need to change the header on the php page to the right filetype before `echo $row['file'];` –  Mar 12 '16 at 14:38
  • Possible duplicate of [Output mp3 with php](http://stackoverflow.com/questions/5275126/output-mp3-with-php) –  Mar 12 '16 at 14:40
  • @Terminus you mean to type 'header("Content-type: text/plain"); ' – hateprogramming Mar 12 '16 at 14:43

2 Answers2

0
<source src="<?php echo '/getAudio.php?id=1/';?>" type="audio/mpeg"> 

You don't need PHP here, juste write it in HTML without the last "/" :

<source src="/getAudio.php?id=1" type="audio/mpeg">

If it does not work, just call "/getAudio.php?id=1" from your browser and see what happens (error ? bad filename ? etc.)

Cédric Nilly
  • 380
  • 1
  • 5
0

Try to integrate or change this in your code:

<?php
header("Content-Type: audio/mpeg");
$song = file_get_contents("01.mp3");//your song directory
?>

<audio controls="controls">
    <source src="<?php echo $song; ?>"  />
</audio>
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
rm_beginners
  • 164
  • 5