Ryan
Ryan

Reputation: 71

Query Working, but unable to echo after running

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");
}

is the function I call, $email and $p_id are correct and I have run the query on the database and it returns the correct value(which is either a 1 or 0)

I want to be able to echo this on the page and then decide whether a checkbox will be checked or not depending on it however when I try and echo after running the function like so:

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

It does not work? Any ideas?

Upvotes: 0

Views: 106

Answers (3)

Last Breath
Last Breath

Reputation: 530

mysql_query return a resource so you must fetch this resource to get the data binded into this resource by using mysql_fetch_array and after that you must go to the new mysqli library because the old mysql library will deprecated soon :)

Try this :

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";
    }

Tell me the result :)

Upvotes: 1

Imane Fateh
Imane Fateh

Reputation: 2406

Try

$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);

Upvotes: 2

Yadav Chetan
Yadav Chetan

Reputation: 1894

Your function should be like this..

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'];

}

it will return array with value try this

Upvotes: 1

Related Questions