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

$_GET['id'] Problems retrieving data

只是尝试使用 php 的 "echo" 粘贴一些值,但运气不好。

我相信我使用的代码在其第一个变量设置“$theid”中存在问题,它从 table 中获取 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'] ?>

问题是,“$data”下没有显示任何内容

在解决此问题并四处查看 SO 之后,我仍未找到答案。非常感谢任何反馈,我确信这只是我使用语法的错误!。谢谢

使用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'];
}

?>
$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 解释为简单引号,因此连接它或不使用 '' 在 PHP 中,var 被解释为双引号。

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

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

命名不正确table