doufuxing8691 2018-10-18 02:07
浏览 32
已采纳

搜索mysql数据库并从php和html中的结果创建链接

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

  • 写回答

1条回答 默认 最新

  • dougu3290 2018-10-18 07:22
    关注

    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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?