Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.