checking return value of max()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
new_php_coder
Forum Newbie
Posts: 2
Joined: Wed Jan 18, 2012 4:26 pm

checking return value of max()

Post by new_php_coder »

Hello all,

I want to check whether

Code: Select all

Code: Select all
 $query="SELECT max( id ) as maxnum FROM `tbl_customer`";
$result=mysql_query($query);
$row = mysql_fetch_array($result);
$myid=$row[maxnum];
returns null/zero. If it is null/zero i.e. no records in tbl_customer then i want id to be 1 else start from 1+id.

I tried

Code: Select all

Code: Select all
if($myid!=""){
      $myid=$myid+1;
   echo "Hello". $myid;
}
else{
   $myid=1;
echo "Hellonull". $myid;
}
Can somebody guide me what is wrong in my code or how to check query with max() result?

Thanks in advance.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: checking return value of max()

Post by social_experiment »

Have you tried $myid = $row[0] ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply