2013-03-29 34 views
0

我正在嘗試基於數據庫中的數據執行一個簡單的youtube搜索。數據正在放入表格中,我希望將歌名命名爲該歌曲的YouTube搜索鏈接。這個想法是將藝術家姓名和歌曲名稱添加到YouTube搜索網址的末尾。我該如何改進我的YouTube搜索功能?

問題是當藝術家名字或歌曲名稱長於一個單詞時,搜索只包含第一個單詞。我用下面的代碼現在:

<table class="table table-striped table-hover table-bordered"> 
    <th>Song Name</th> 
    <th>Artist</th> 
<?php 
// Create connection 
$con=mysqli_connect("localhost","root","checkout","Music Database"); 

// Check connection 
if (mysqli_connect_errno($con)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$result = mysqli_query($con,"SELECT * FROM MusicDB"); 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr><td><a href=https://www.youtube.com/results?hl=en&q=".$row['Song']."+".$row['Artist'].">".$row['Song']."</a></td>"; 
    echo "<td>".$row['Artist']."</td>"."</tr>"; 
    } 


mysqli_close($con); 

?> 
</table> 

例如,如果歌曲是「垮掉」的「邁克爾·傑克遜」,它只會增加「拍」到搜索。

如果這首歌是「Thriller」,它會添加「Thriller + Michael」。

如果我在「Maclemore」中添加歌曲「Thrift Shop」,它只會爲搜索添加「節儉」。

任何想法或想法將不勝感激。

回答

0

使用urlencode來編碼查詢字符串中的值。

"<tr><td><a href=https://www.youtube.com/results?hl=en&q=". urlencode($row['Song'] . " " . $row['Artist']).">".$row['Song']."</a></td>"; 

您也可以使用http_build_query

"<tr><td><a href=https://www.youtube.com/results?" . http_build_query(array(
    "hl" => "en", 
    "q" => $row['Song'] . " " . $row['Artist'] 
)).">".$row['Song']."</a></td>"; 
+0

謝謝!我並不知道這些功能。我非常感謝你的幫助。 – bornytm