0

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

charlie
  • 415
  • 4
  • 35
  • 83
  • Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems you have run into? Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 07 '15 at 13:01
  • `if(condition){ do something } else{ do something else }` *right Sam?* - @JayBlanchard – Funk Forty Niner May 07 '15 at 13:04
  • *Right Ralph!* @Fred-ii- – Jay Blanchard May 07 '15 at 13:07

3 Answers3

1

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.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
0

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");
}
Peter
  • 8,776
  • 6
  • 62
  • 95
0

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...
}
marijnz0r
  • 934
  • 10
  • 23