Jump to content

[SOLVED] Select Top 1* returns multiple results!


wellscam

Recommended Posts

Hey,

 

I'm querying an Access database using PHP with this query to return the last entry in the database and display how current it is.

 

$query = odbc_exec($odbc, "SELECT Top 1 * FROM AllInfo WHERE IO = 'Out' AND CO = '51' ORDER BY [DateTime] DESC") or die (odbc_errormsg()); 
while($row = odbc_fetch_array($query)) 
{ 
    echo 'Database current as of<strong> '.date("F j, Y, g:i a", strtotime($row['DateTime'])).'</strong> and will update every 23 minutes.'; 
} 

 

The problem is, if there are 2 or 3 identical records in there, it will return all of those records. How do I tell it to only return the first one? Or maybe add a second order by parameter?

Link to comment
Share on other sites

$query = odbc_exec($odbc, "SELECT Top 1 * FROM AllInfo WHERE IO = 'Out' AND CO = '51' ORDER BY [DateTime] DESC LIMIT 0,1") or die (odbc_errormsg()); 

 

Returns:

[Microsoft][ODBC Microsoft Access Driver] Syntax error in ORDER BY clause.

Link to comment
Share on other sites

When I try this,

$query = odbc_exec($odbc, "SELECT Top 1 * FROM AllInfo WHERE IO = 'Out' AND CO = '51' ORDER BY [DateTime, Extension] DESC") or die (odbc_errormsg()); 

 

I get this error:

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

 

I understand that there shouldn't be identical rows. Unfortunately, the database is being generated by a software program that I have no control over and it does not add a unique id for each row. It's a phone program that queries and logs our switch data into an Access database with simple data like Extension, Time, Date, Line Used, Incoming/Outgoing, Total Duration etc...

 

My result sometimes repeats a couple of times and I'm thinking that it's because there are multiple calls with the same timestamp, is there another reason my query would produce the result twice?

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.