2014-07-21 39 views
-2

我編寫了此代碼以從圖像鏈接數據庫中提取,以便更新天氣鏈接。但是,它不會將圖像鏈接打印到urlspace中:無法訪問mysql數據庫並打印出匹配結果

<?php 
/* This code establishes MySQL connection,creates a function that pulls URLs from database to update picture file ? */ 

$datab ="weather"; 
$connect = new PDO("mysql:host=localhost;dbname=$datab", "***", "*****"); 
function rainmaker($fname) { 
$sql = $connect->query("SELECT link FROM `radar3` WHERE `name` = `$fname`"); 
while($result = $sql->fetch(PDO::FETCH_ASSOC)){ 
$url1 = urlencode($result['name']); 
print $url1; 
}; 
?> 


    //URL where the image link will be printed: 

     <P><A HREF="../showImage.php?&url=<?=rainmaker('**$fname**')?>.php">North Plains</A></P> 

相反,它只是打印php函數名稱代碼。

+1

[**在新的代碼,請不要使用'mysql_ *'功能**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – esqew

+0

對於新的開發,你應該使用mysqli擴展,而不是mysql_擴展)。從php.net(http://php.net/manual/en/intro.mysql.php)開始:「這個擴展從PHP 5.5.0開始已被棄用,不推薦用於編寫新代碼,因爲它將被刪除未來,而應該使用mysqli或PDO_MySQL擴展。「 – mti2935

+1

你的代碼是錯誤的部分,你實際上從你的SQL查詢中得到結果。請檢查'mysqli_fetch_assoc'或類似的東西。 http://php.net/manual/en/mysqli-result.fetch-assoc.php – ToBe

回答

3

使用反引號逃逸表和列名,沒有引號

SELECT link FROM `radar3` WHERE `name` = '$fname' 

只能使用引號中查詢靜態字符串。

+1

也可以在這裏建議不要使用短標籤'<?='。 – Prasanth