-1

我正在尝试构建一个简单的搜索,该搜索首先从通过 url 从 HTML 表单传递到该脚本的表单中获取“查询”。运行脚本后,我会得到输出:Resource id #140Resource id #141Resource id #142。为什么我会得到这个输出,这是什么意思?

旁注我只是使用“回声”作为查看每个变量输出的一种方式。

<?php 
//connect to database
mysql_connect("localhost", "user", "password") or die("Error connecting to database: " .mysql_error());

mysql_select_db("dataBase") or die(mysql_error());

?>
<?php
$query = $_GET['query']; 
// gets value sent over search form

$user_id = mysql_query("SELECT id FROM users WHERE email = '$query'") or die(mysql_error());
echo $user_id;

$account_id = mysql_query("SELECT 'account_id' FROM accounts_users WHERE 'user_id' LIKE ('$user_id')") or die(mysql_error());
echo $account_id;

$user_name = mysql_query("SELECT 'account_name' FROM accounts WHERE 'id' LIKE ('$account_id')") or die(mysql_error());
echo $user_name;    
?>
4

3 回答 3

2

这不是打印结果的方式。该方法mysql_query返回一个资源,您必须在循环中使用该资源才能实际打印结果。例如,在官方文档页面中的第二个示例中循环。

PS$query = $_GET['query'];使用此语句可能会遇到Sql 注入问题。

于 2012-05-03T08:22:11.973 回答
1

尝试类似的事情 - 在第一次“SELECT”查询之后:

    while($user_id_obj = mysql_fetch_object($user_id))
    {
      echo $user_id_obj->id;
    }
于 2012-05-03T08:27:22.687 回答
0

这可以通过两种方式实现。哪种方式对您有用取决于您的要求。

1.如果您的查询结果包含一个值,那么在您的代码中进行更改的以下代码将对您有用。

<?php 
//connect to database
mysql_connect("localhost", "user", "password") or die("Error connecting to database: " .mysql_error());

mysql_select_db("dataBase") or die(mysql_error());

?>
<?php
$query = $_GET['query']; 
// gets value sent over search form

$result_user = mysql_query("SELECT id FROM users WHERE email = '$query'") or die(mysql_error());
if (!$result_user) {
    die('Could not query:' . mysql_error());
}
$user_id=mysql_result($result_user,0); // outputs first user's id
echo $user_id;
$result_accountuser = mysql_query("SELECT 'account_id' FROM accounts_users WHERE 'user_id' LIKE ('$user_id')") or die(mysql_error());
if (!$result_accountuser) {
    die('Could not query:' . mysql_error());
}
$account_id=mysql_result($result_accountuser,0); // outputs first accounts_users's account_id
echo $account_id;
$result_account = mysql_query("SELECT 'account_name' FROM accounts WHERE 'id' LIKE ('$account_id')") or die(mysql_error());
if (!$result_account) {
    die('Could not query:' . mysql_error());

}
echo mysql_result($result_account,0); // outputs first accounts's account_name
?>

2.或者您的查询包含多个结果或多个行,而不是您的代码中的以下更改将对您有所帮助

<?php 
//connect to database
mysql_connect("localhost", "user", "password") or die("Error connecting to database: " .mysql_error());

mysql_select_db("dataBase") or die(mysql_error());

?>
<?php
$query = $_GET['query']; 
// gets value sent over search form

$result_user = mysql_query("SELECT id FROM users WHERE email = '$query'") or die(mysql_error());
        while($row=mysql_fetch_array($result_user))
        {
                $user_id = $row['id'];
                echo    $user_id;
        }


$result_accountuser = mysql_query("SELECT 'account_id' FROM accounts_users WHERE 'user_id' LIKE ('$user_id')") or die(mysql_error());
        while($row=mysql_fetch_array($result_accountuser))
        {
                $account_id = $row['account_id'];
                echo $account_id;

        }

$result_account = mysql_query("SELECT 'account_name' FROM accounts WHERE 'id' LIKE ('$account_id')") or die(mysql_error());
        while($row=mysql_fetch_array($result_account))
        {
                echo $row['account_name'];

        }

?>
于 2012-05-03T08:41:16.407 回答