Jump to content

Having an issue with LIMIT when trying to select most recent record


mongoose00318

Recommended Posts

I've been having issues with this since Friday and still haven't resolved it. If I call that function with a $order_id set to 7601 and the SQL clause says "LIMIT 1" I get a blank array that comes back.

If I change it to LIMIT 2; I get the result I expected...but not two records...just the one? There are no blank records in the DB from what I see and if I run that same query in PHPMyAdmin it returns the results I expect?

//build LED request buttons
function build_led_request_btn($order_id, $pdo) {
	
	//info to gather
	//$order_id, $job_number, $enterprise, $request_id, $request_status
	
	//get request status
	$query = "SELECT * FROM leds_requests WHERE order_id = :order_id ORDER BY id DESC LIMIT 1";
	$statement = $pdo->prepare($query);
	$statement->execute(['order_id' => $order_id]);
	$count_records = $statement->rowCount();
	
	return $statement->fetch()['status_id'];
	exit();

}

image.png.81f260dc28de2cf4ad026e173a1878c6.png

Link to comment
Share on other sites

32 minutes ago, mongoose00318 said:

I get a blank array that comes back

how do you know that, especially since you should be getting a status_id number as the returned value? perhaps there's some problem with the code that's  using the result from calling that function?

what is the (default) fetch mode you are using for the ->fetch() method call? have you set a default fetch mode yourself or are you using the default, default fetch mode?

any chance you have more than one database and are selecting the wrong one?

 

Edited by mac_gyver
Link to comment
Share on other sites

@Barand There was additional code below the exit() that I thought was irrelevant to the post and I was trying to keep it short for everyone. Turns out the problem was in the code below the exit(); lol...it was there for debugging purposes. The comment is old; I originally was going pass all that to the function; but instead I just pass the order_id to it now. The rest of is provided elsewhere from other parts of the application. I should have the function buttoned up sometime today and I'll post it for review. It's a part of a big new feature I'm building into the application. I'm looking forward to getting it out to the end user.

Link to comment
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.