mysql错误资源ID#5

时间:2012-10-02 07:44:16

标签: php mysql

我有这个小代码片段,但它现在还没有真正起作用。我总是有资源ID#5错误。我在谷歌搜索了几个指针,但没有发现mutch通过这个问题。这是我的代码

<?php
include 'load_db.php';

$var1 = $_POST["gender_1"];
$var2 = $_POST["gender_2"];
$var3 = $_POST["age"];

$sql = mysql_query("SELECT url FROM links WHERE gender ='".$var1."' AND gender1 ='".$var2."' AND age ='".$var3."'");

$result_1 = mysql_query($sql)
OR die("Error: $sql </br>".mysql_error());

echo $result_1;
?>
  

错误:资源ID#5

2 个答案:

答案 0 :(得分:4)

您正在使用mysql_query()两次,从mysql_query()删除$sql

$var1 = mysql_real_escape_string($_POST["gender_1"]);
$var2 = mysql_real_escape_string($_POST["gender_2"]);
$var3 = mysql_real_escape_string($_POST["age"]);

$sql = "SELECT url FROM links WHERE gender ='".$var1."' AND gender1 ='".$var2."' AND age ='".$var3."'";
$result_1 = mysql_query($sql) OR die("Error: $sql </br>".mysql_error());

然后循环结果:

while($row = mysql_fetch_array($result_1)){
    print_r($row);
}

答案 1 :(得分:0)

一些指示:

  1. 虽然您创建了3个局部变量var1,var2,var3,但您仍然可以使用mysql插件提供的漏洞。您应该查看这个。
  2. 你的代码:
  3. $sql = mysql_query("SELECT url FROM links WHERE gender ='".$var1."' AND gender1 ='".$var2."' AND age ='".$var3."'"); 
    $result_1 = mysql_query($sql)  OR die("Error: $sql </br>".mysql_error());
    

    你两次调用sql_query ..

    将$ sql更改为

    $sql = "SELECT url FROM links WHERE gender ='".$var1."' AND gender1 ='".$var2."' AND age ='".$var3."'";