0

I am a beginner to PHP. I have a database set up with songs in it. At the moment there are only 2 songs and one artist. I am trying to query the database by artist. The page seems to work but is only returning one song instead of two. I am calling it like this :

search by artist

What is the correct way to do this?

    <?php
    // get artist id from page call
    $artist = $_GET['artist'];
    // search by artist
    $exists = $mysqli->query("SELECT id FROM songs WHERE artist='$artist'") or die($mysqli->error);
    // get numeric array out of result
    $Songs = mysqli_fetch_array($exists, MYSQLI_NUM);


   foreach($Songs as $key){

     echo "<a href='http://www.waylostreams.com/login-system/playSong.php?id=$key&user=$user_id'>Listen</a>";
     print "<br>";

     }

    ?>

Thanks in advance for any help! Sean

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Sean Wayland
  • 39
  • 1
  • 1
  • 8
  • First of all, you should NEVER user unfiltered input in your SQL. It's a major security flaw. Read this: http://php.net/manual/en/mysqli.real-escape-string.php – Yoshimitsu Oct 18 '18 at 02:16

1 Answers1

0

This is an example. You can use this code to select all elements in your table(w3schools) nere the link where explain step by step: https://www.w3schools.com/php/php_mysql_select.asp

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = newmysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
Ferdinando
  • 964
  • 1
  • 12
  • 23