timmah1 Posted February 19, 2009 Share Posted February 19, 2009 I select items from the database, and if numrows = 0, I want to automatically use the last id in the database. My code is here $Ddate = date("Y-m-d"); $Dtime = date("g:ia"); $query1 = "SELECT * FROM specials WHERE sport = '$sport' AND startdate = '$Ddate' AND starttime <= '$Dtime' AND endtime >= '$Dtime'"; $w = mysql_query($query1); $numrows = mysql_num_rows($w); if($numrows == 0){ echo "nothing"; } So say in the database, there are id's 2 and 3. If numrows equals 0, I'd like to show what's in the database for id number 3. How would I do this? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/145864-solved-show-last-id/ Share on other sites More sharing options...
MasterACE14 Posted February 19, 2009 Share Posted February 19, 2009 try changing this... <?php $query1 = "SELECT * FROM specials WHERE sport = '$sport' AND startdate = '$Ddate' AND starttime <= '$Dtime' AND endtime >= '$Dtime'"; to this... <?php $query1 = "SELECT * FROM specials WHERE sport = '$sport' AND startdate = '$Ddate' AND starttime <= '$Dtime' AND endtime >= '$Dtime' ORDER BY `id` ASC LIMIT 1"; <?php $Ddate = date("Y-m-d"); $Dtime = date("g:ia"); $query1 = "SELECT * FROM specials WHERE sport = '$sport' AND startdate = '$Ddate' AND starttime <= '$Dtime' AND endtime >= '$Dtime' ORDER BY `id` ASC LIMIT 1"; $w = mysql_query($query1); $row = mysql_fetch_array($query1); $numrows = mysql_num_rows($w); if($numrows == 0){ echo $row['some_field']; } EDIT: on second thought, if it returns zero rows, you need to add a second query to grab just the 1 result. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/145864-solved-show-last-id/#findComment-765821 Share on other sites More sharing options...
timmah1 Posted February 19, 2009 Author Share Posted February 19, 2009 And that would do nothing if nothing matches the database. Numrows would still come back as 0 Quote Link to comment https://forums.phpfreaks.com/topic/145864-solved-show-last-id/#findComment-765825 Share on other sites More sharing options...
timmah1 Posted February 19, 2009 Author Share Posted February 19, 2009 oh i see now Quote Link to comment https://forums.phpfreaks.com/topic/145864-solved-show-last-id/#findComment-765826 Share on other sites More sharing options...
sasa Posted February 19, 2009 Share Posted February 19, 2009 $query1 = "(SELECT * FROM specials WHERE sport = '$sport' AND startdate = '$Ddate' AND starttime <= '$Dtime' AND endtime >= '$Dtime') UNION (SELECT * FROM specials ORDER BY id DESC LIMIT 1) LIMIT 1"; Quote Link to comment https://forums.phpfreaks.com/topic/145864-solved-show-last-id/#findComment-765863 Share on other sites More sharing options...
timmah1 Posted February 19, 2009 Author Share Posted February 19, 2009 thank you both Quote Link to comment https://forums.phpfreaks.com/topic/145864-solved-show-last-id/#findComment-765874 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.