【问题标题】:Search mysql database and create links from result in php and html搜索 mysql 数据库并从 php 和 html 中的结果创建链接
【发布时间】:2018-10-18 02:07:22
【问题描述】:

我是 PHP 的初学者。我有一个数据库,里面有歌曲。目前只有 2 首歌曲和 1 位艺术家。我正在尝试按艺术家查询数据库。该页面似乎有效,但只返回一首歌曲而不是两首。 我这样称呼它:

search by artist

这样做的正确方法是什么?

    <?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>";

     }

    ?>

提前感谢您的帮助! 肖恩

【问题讨论】:

标签: php html mysql mysqli


【解决方案1】:

这是一个例子。您可以使用此代码选择表(w3schools)中的所有元素,靠近逐步解释的链接: 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();
?>

【讨论】:

    猜你喜欢
    • 2017-05-21
    • 2016-04-07
    • 2021-01-07
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2017-11-09
    相关资源
    最近更新 更多