samjones39
samjones39

Reputation: 163

How to redirect or force page if mysql returns nothing in a query

working on a small project and having a small problem. i am querying mysql, i am able to get the array and everything works well but i am adding a additional condition if returns 0 or id does not match the query then redirect.

This is what i have so far.

$q = "SELECT TEST.*,
             EMPLOYEE.EMP_ID                     
        FROM TEST
        LEFT JOIN EMPLOYEE ON TEST.EMP_ID = EMPLOYEE.EMP_ID 
        WHERE ITEM_ID=".$db->qstr($item_id);

if(!$item_id = $db->execute($q)){
    force_page('system', 'Nothing found');
    exit;
} else {
    $view = $item_id->GetArray();
}

return $view;
}

so the above returns the array, query grabs $item_id from the url so it could be anything. i would like to check if that id exists if not then force the page as shown in the above. any suggestion on what i am doing wrong?

Upvotes: 0

Views: 372

Answers (4)

If you want to redirect you can use header function

header("Location: put_your_url_here");

e.g.

header('Location: /mypage.php');

Upvotes: 1

Nabin Kunwar
Nabin Kunwar

Reputation: 1996

I don't think there is any function like force_page(); in php You need to redirect like header("Location: Your_url");

And If you have function force_page(); please post code of that.

Upvotes: 0

Suchit kumar
Suchit kumar

Reputation: 11859

header("Location: Your_url"); will help you to redirect based on your condition.but make sure that you have not echoed any thing before the header otherwise it won't redirect.

Upvotes: 0

Basit
Basit

Reputation: 1840

There is no function such as force_page in PHP. You can use header to redirect user to another page. Modify your if condition as

if(!$item_id = $db->execute($q)){
    header("Location: url_to_redict");
    die();
}

Upvotes: 0

Related Questions