Jump to content

[SOLVED] Show last id


timmah1

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/145864-solved-show-last-id/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/145864-solved-show-last-id/#findComment-765821
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.