charlie
charlie

Reputation: 481

PHP/MySQL Search results. Auto redirect if only one result

I have multiple SQL Queries with While loops in PHP for a search form.

There are 3 blocks of code in total, each selected from a different table and before the while loop i use

if(mysql_num_rows($rs) > 0) {
    ...show results here
}

I would like to have it so if there is only one result returned for the 3 blocks of code, it will automatically redirect to a different page.

For example, i am searching for Customers, Contacts and Invoices. So they are the 3 different blocks of code / queries i have.

If i search for Company A and there is 1 record returned in the customers query and 0 records returned in Contacts and Invoices, it should redirect to the customer page. However if there is 2 records returned it should just display the search results.

and the same for Customers, Contacts and Invoices

Upvotes: 0

Views: 165

Answers (3)

marijnz0r
marijnz0r

Reputation: 934

This is extremely simplified code, and can be highly optimized, but I think this is what you're looking for.

//After queries you've got 3 arrays:
$customers = [...];
$contacts = [...];
$invoices = [...];

//Determine if there's only 1 result of 1 category returned from the queries:
if(sizeof($customers)==1 && sizeof($contacts)==0 && sizeof($invoices)==0){
// Redirect to customers...
} elseif(sizeof($customers)==0 && sizeof($contacts)==1 && sizeof($invoices)==0) {
// Redirect to contacts...
} elseif(sizeof($customers)==0 && sizeof($contacts)==0 && sizeof($invoices)==1){
// Redirect to invoices...
} else {
// Redirect to results page...
}

Upvotes: 0

George Cummins
George Cummins

Reputation: 28926

To redirect to a new page, use header():

header("Location: /url/to/company/page?id=$companyId");

Your conditional needs to handle at least three situations: No results, a single result, and multiple results:

switch (mysql_num_rows($rs)) {
    case 0:
        $this->noResultsFound();
        break;
    case 1:
        header("Location: /url/to/company/page?id=" . $rs[0]['companyId']);
        break;
    default:
        $this->showResults($rs);
        break;
 }

As stated in the comments, the mysql_* functions are deprecated; they have been replaced by better, safer alternatives. Consider investigating the benefits of PDO.

It is generally bad practice to use die() statements in production code. However, one of the rare exceptions is in conjunction with the use of header(). It may not be obvious, but when you redirect with header(), the rest of your script continues to run before the HTTP header with the redirect instruction is sent to the browser. In the case of poorly-structured code, this can lead to unexpected results.

To avoid that problem, insert a die() statement immediately after the header() line.

Upvotes: 1

Peter
Peter

Reputation: 9123

Wouldn't the following suffice?

if (mysql_num_rows($result) == 1) {
    header("Location: other_script.php");
} elseif (mysql_num_rows($result) > 1) {
    // ... show results ...
} else {
    // ... no results ...
}

In case I understood your question wrong and you have multiple queries but want to know the total results, thats as simple as:

$one = mysql_num_rows($result_one);
$two = mysql_num_rows($result_two);
$three = mysql_num_rows($result_three);
$total = $one + $two + $three;
if ($total == 1) {
    header("Location: other_page.php");
}

Upvotes: 0

Related Questions