2017-02-28 22 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