Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 22484

PHP detecting no result for PDO query

$
0
0

I am using PHP to fetch and determine the highest ID number for a category, then create the next id in order. The IDs are alphanumeric, a two letter code plus the numerical count ie: TH2, FR23.

Using the PDO code below, I am successfully able to retrieve the highest numbered ID for the category and increment it by one. But if there are no IDs using the letter pair, the process fails because the $hotelID variable is not generated. If the letter pair doesn't exist in the database yet it should generate the first ID ie: TH1 or FR1.

My SQL:

// find highest existing id code with that two letter code$newHtlId = $offbase->query("SELECT * FROM hotelMstr WHERE hotelID LIKE '%$cntrycode%' ORDER BY hotelID DESC LIMIT 1");while ($htlID = $newHtlId->fetch(PDO::FETCH_ASSOC)) {    if ($newHtlId->rowCount() == 0) {        $hotelID = $cntrycode.'1';    }    else {        $hotelID = ++$htlID['hotelID'];    }}

The $cntrycode is the two letters shown in the id examples.


Viewing all articles
Browse latest Browse all 22484

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>