Jump to content

SELECT TOP equivalent in MySQL


ramone_johnny

Recommended Posts

Can someone tell me if this is right?

$r2=query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = ?",$rstDBEdit["share_ID"]). " ORDER BY tblph_shareID DESC LIMIT 1";

I'm getting the following error....

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in (line 35)

 

....which is

if($rstDBEdit2=mysql_fetch_assoc($r2))

Here's the code in it's entirity

$r2=query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = ?",$rstDBEdit["share_ID"]). " ORDER BY tblph_shareID DESC LIMIT 1";
	
	if($rstDBEdit2=mysql_fetch_assoc($r2))
	{
		$fname = $rstDBEdit2["tblph_filename"]; 
		$id = $rstDBEdit2["tblph_shareID"];
		$image = $blogpath.$rstDBEdit["tblph_ID"]."/small_".$fname;
		$virtualPath = $shareurl.$rstDBEdit["tblph_ID"]."/small_".$fname;
Link to comment
https://forums.phpfreaks.com/topic/276893-select-top-equivalent-in-mysql/
Share on other sites

Putting aside the fact that you put the ORDER BY stuff in the wrong place (you're concatenating it with the share_ID, not with the query), apparently query_wrapper() returned a string. Why is that? What does the function do? What's the code?

 

[edit] And to answer the question in the title and not in your post... not even close to what's in your post... T-SQL's TOP N is equivalent to MySQL's LIMIT N. As it seems you've found out.

Would you mind helping me get this query right?

 

My background is ASP/MSSQL.

 

This is what I currently have.

 

$share_ID = $rstDBEdit["share_ID"];
$r2=query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = ", $share_ID. " ORDER BY tblph_ordering ASC LIMIT 1");

Yeah, you've got the right overall query, but look at that function call more carefully.

query_wrapper("SELECT * FROM tblphotos WHERE tblph_shareID = ", $share_ID. " ORDER BY tblph_ordering ASC LIMIT 1")
You're passing two arguments. The first one is SELECT * FROM tblphotos WHERE tblph_shareID = and the second one (if $share_ID=123) is 123 ORDER BY tblph_ordering ASC LIMIT 1. That's surely not how the function is supposed to work.

 

However the function returns a string. That's odd. What does the function do and how is it supposed to work?

You had the right order before, it just wasn't all together.

SELECT * FROM tblphotos WHERE tblph_shareID = ? ORDER BY tblph_shareID DESC LIMIT 1
If that doesn't work, please post the code for query_wrapper().

 

[edit] Third edited post in a row :(

 

Your query is wordier than it needs to be: you're searching by the shareID so there's no point sorting by it.

SELECT * FROM tblphotos WHERE tblph_shareID = ? LIMIT 1
And if the tblph_shareID is unique in the table then you don't even need the LIMIT.

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.