Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

Handling ZERO Results in a PHP PDO Query

Hi I have a PDO connection to a PostgreSQL database

How do I run a Full back query if the Preffered query has zero results

I’m doing something like this

try
{
      $pdo = PDO(Enter DB Connect details);
}
catch (PDOException $exec)
{
  trigger_error($exec->getMessage(), E_USER_ERROR);
}

$sql = <<<SQL
select stuff from table where "MayBe" = 'I might be here'  and
"always" = ?
SQL;

$sth= $pdo->prepair($sql);
$execute(array($_POST['MySearchVal'];
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

// if no results run another query before continuing


foreach($result as $row)
 {
      // have results Do Something
  }

Open in new window


Is this the  best way to do this or is there a MYSQLi for PostgreSQL?
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sidebar note:  If you have not extended the class to add a new method, this misspelling might be worth a look:
$sth= $pdo->prepair($sql);

Open in new window

http://php.net/manual/en/pdo.prepare.php
Avatar of trevor1940
trevor1940

ASKER

Hi Guys FYI I'll be looking at this again on Monday

In the meantime the link that ray pointed to and the docs suggest the need to do a select count(*) to accurately get the number of rows and then execute which ever query returns data.

In this context that is possibly the correct solution however in other cases do i use what Chris said or   $num     = $pdos->rowCount();?

PS. Apologize for the typo
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologies for the delay

Managed to sort the query out thanx