查询工作,但运行后无法回显

时间:2013-04-17 18:32:03

标签: php mysql

function supervisor_from_email($email, $p_id) {
    $email = sanitize($email);
    return mysql_query("SELECT pm.supervisor FROM project_members AS pm JOIN users AS u ON pm.project_member_id = u.user_id WHERE u.email = $email AND pm.project_id = $p_id");
}

是我调用的函数,$ email和$ p_id是正确的,我在数据库上运行查询并返回正确的值(为1或0)

我希望能够在页面上回显这个,然后决定是否选中复选框,具体取决于它,但是当我在运行这样的函数后尝试回显时:

$supervisor = supervisor_from_email($email, $p_id);
echo $supervisor;

它不起作用?有什么想法吗?

3 个答案:

答案 0 :(得分:2)

尝试

$result = mysql_query("SELECT pm.supervisor FROM project_members AS pm JOIN users AS u ON pm.project_member_id = u.user_id WHERE u.email = '$email' AND pm.project_id = $p_id");
if (!$result) 
{
die(mysql_error()); 
}

while($row = mysql_fetch_array($result))
{
echo $row['supervisor'];
}

OR

$result = mysql_query("SELECT pm.supervisor FROM project_members AS pm JOIN users AS u ON pm.project_member_id = u.user_id WHERE u.email = '$email' AND pm.project_id = $p_id");
if (!$result) 
{
die(mysql_error()); 
}
mysql_result($result,0);

答案 1 :(得分:1)

mysql_query返回一个资源,因此您必须使用mysql_fetch_array获取此资源以将数据绑定到此资源中,之后您必须转到新的mysqli库,因为旧的mysql库很快就会弃用:)

试试这个:

function supervisor_from_email($email, $p_id)
    {
        $email = sanitize($email);
        $email_rs = mysql_query("SELECT pm.supervisor FROM project_members AS pm JOIN users AS u ON pm.project_member_id = u.user_id WHERE u.email = '$email' AND pm.project_id = $p_id");
        if(is_resource($email_rs))
            {
                $email_res = mysql_fetch_array($email_rs);
                $email = $email_res[0];
                return $email;
            }
        else
            return "Invalid supervisor";
    }

告诉我结果:)

答案 2 :(得分:1)

你的功能应该是这样的..

function supervisor_from_email($email, $p_id) {
    $email = sanitize($email);
    $result=mysql_query("SELECT pm.supervisor FROM project_members AS pm JOIN users AS u ON pm.project_member_id = u.user_id WHERE u.email = $email AND pm.project_id = $p_id");
   $row=mysql_fetch_assoc($result);
    return $row['supervisor'];

}

它将返回带有值的数组试试这个