$ _GET [' id']检索数据时出现问题

时间:2015-01-15 01:40:26

标签: php sql arrays database isset

只是尝试使用php" echo"来粘贴一些值。并且我运气不好。

我相信我使用的代码在其第一个变量设置" $ theid"中有问题,它从表中获取id字段。这是代码:

<?php

$conn=mysql_connect("localhost", "username", "password")
or die ("Cannot Connect to MYSQL");

mysql_select_db("database1", $conn)
or die ("Cannot Connect to the Database");
echo "Connected successfully";

$theid = isset($_GET['id'])?$_GET['id']:""; //Possible problematic code

$data = mysql_fetch_array(mysql_query("SELECT * FROM table1 WHERE id='$theid'"));

?>

然后将使用数据:

<?php echo $data['url'] ?>

问题是,&#34; $ data&#34;

下没有显示任何内容

在对此进行故障排除并环顾SO之后,我仍然没有找到答案。任何反馈都非常感谢,我确信这只是我使用语法时出错!感谢。

3 个答案:

答案 0 :(得分:0)

使用while循环打印查询结果数据

<?php

$conn=mysql_connect("localhost", "username", "password")
or die ("Cannot Connect to MYSQL");

mysql_select_db("database1", $conn)
or die ("Cannot Connect to the Database");
echo "Connected successfully";

if(isset($_GET['id'])){
   $theid = $_GET['id'];
}

$result = mysql_query("SELECT * FROM table1 WHERE id='$theid'");

while($data = mysql_fetch_array($result)){
    echo $data['url'];
}

?>

答案 1 :(得分:0)

$conn=mysql_connect("localhost", "username", "password")
or die ("Cannot Connect to MYSQL");

mysql_select_db("database1", $conn)
or die ("Cannot Connect to the Database");
echo "Connected successfully";

//default value
$theid = 0;
if(isset($_GET['id'])){
   $theid = $_GET['id'];
}

$result = mysql_query("SELECT * FROM table1 WHERE id='".(int)$theid."'");

while($data = mysql_fetch_array($result)){
    echo $data['url'];
}

转换为int以防止SQL注入, 并且var不能被解释为简单引用或不使用&#39;&#39; 在PHP中,var被解释为双引号。

答案 2 :(得分:0)

$result = mysql_query("SELECT * FROM table2 WHERE id='$theid'");

while($data = mysql_fetch_array($result)){
    echo $data['url'];

错误地命名表