2017-02-28 27 views
0

在mysql表中有3列id,name,addr id是自動增量& name和addr有值,我想用get方法得到結果,但它只是顯示解析錯誤。 。 。 。php變量在mysql中使用get方法

<?php 
$id = $_GET['id']; 
if (empty($_GET['id']) || !is_numeric($_GET['id'])) { 
    echo 'A valid image file id (numeric) is required to display the image file.'; 
    exit; 
} else { 
    $query = mysql_query("SELECT * FROM imgtable where id={$id}"); 
    while ($res = mysql_fetch_array($query)) 
    { 
?> 
<tr><td> <?php echo $res['name']; ?></td><td><img src="<?php echo $res['addr']; ?>" width='200px' height='200px'/></td></tr> 
<?php}} ?> 
+3

什麼是錯誤? – chris85

+3

mysql_ *函數已在PHP7中刪除,並且在PHP5.3之後棄用。我建議切換到[PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php)或[mysqli](http://php.net/manual/en/mysqli .quickstart.prepared-statements.php)。它也將在這裏修復你的SQL注入漏洞。 – aynber

+0

**警告**:如果您只是學習PHP,請不要使用['mysql_query'](http://php.net/manual/en/function.mysql-query.php)界面。這是非常可怕和危險的,它在PHP 7中被刪除了。[PDO的替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)以及[PHP The Right Way](http://www.phptherightway.com/)等指南介紹了最佳實踐。你的用戶數據是**不是** [正確轉義](http://bobby-tables.com/php.html),並有[SQL注入漏洞](http://bobby-tables.com/),並且可以被利用。 – tadman

回答

0

試試這個:

$query = mysqli_query("SELECT * FROM imgtable where id='$id'"); 

相反

$query = mysql_query("SELECT * FROM imgtable where id={$id}"); 

。顯然,如果切換到mysqli,也是在followng使用mysqli_fetch_array

+0

雖然'mysqli'是一個更好的主意,PDO更好,但這個「修復」不太可能改變任何有意義的東西。 – tadman

+0

'mysqli_query'也需要連接。 – chris85