2

I will create a website in that website admin upload some audio files in the server everything is work perfect but I was uploaded a PHP file in that file I will fetch all the data (Name of file and URL of that file) from the database but I don't know how I can put that data into my HTML code. In HTML when the user clicks the name then URL should load and play music.

<?php
 define('DB_HOST','localhost');
 define('DB_USERNAME','Root');
 define('DB_PASSWORD','');
 define('DB_NAME','audiofiles');

 //connecting to the db
 $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or 
 die("Unable to connect");

 //sql query 
 $sql = "SELECT * FROM audio";

//getting result on execution the sql query
$result = mysqli_query($con,$sql);

//response array
$response = array();

$response['audio'] = array();

//traversing through all the rows

 while($row =mysqli_fetch_array($result)){
 $temp = array();
 $temp['id'] = $row['id'];
 $temp['name'] = $row['name'];
 $temp['url'] = $row['url'];
 array_push($response['audio'],$temp);
 }

This is my PHP of fetching audio files from the database. I want some help to display that audio file in the HTML page and also play that files. I want to do that PHP file should grab that name and link of the audio file which will work perfectly now I want HTML to embed name and the link as the src.

halfer
  • 19,824
  • 17
  • 99
  • 186
Aakash Jain
  • 87
  • 10

1 Answers1

3

Just use <audio> HTML tag inside while() loop like below:-

while($row =mysqli_fetch_array($result)){?>
    <?php echo $row['name'];?> : <audio controls><source src="<?php echo $row['url'];?>"></audio><br>
<?php }?>

So full code needs to be:-

<?php
 define('DB_HOST','localhost');
 define('DB_USERNAME','Root');
 define('DB_PASSWORD','');
 define('DB_NAME','audiofiles');

 //connecting to the db
 $con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or 
 die("Unable to connect");

 //sql query 
 $sql = "SELECT * FROM audio";

//getting result on execution the sql query
$result = mysqli_query($con,$sql);

//traversing through all the rows

while($row =mysqli_fetch_array($result)){?>
    <?php echo $row['name'];?> : <audio controls><source src="<?php echo $row['url'];?>"></audio><br>
<?php }?>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98