Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4608614
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:52:12+00:00 2026-05-22T00:52:12+00:00

To select last record, I was using this when my database was MySQL: $result

  • 0

To select last record, I was using this when my database was MySQL:

$result = mysql_query("SELECT Id 
                         FROM test 
                     ORDER BY LENGTH(Id), Id ASC");

$count = mysql_numrows($result);

if($count != 0) {    
  $lastid = mysql_result($result,$count-1,"Id");    
}

…and it works fine.
But now my table is in an Oracle database – I wrote:

$stid = oci_parse($conn, "SELECT Id 
                            FROM test 
                        ORDER BY LENGTH(Id), Id ASC");

oci_execute($stid);

$p = oci_parse($conn, "SELECT COUNT(ID) 
                         FROM test");

oci_execute($p);

$count = oci_fetch_array($p);

if($count[0] != 0) {    
  $lastid = oci_result($stid, $count[0]-1);
}

It doesn’t work – I am not getting the last record.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:52:13+00:00Added an answer on May 22, 2026 at 12:52 am

    If you read the documentation for oci_result, you’ll find you’re using the second parameter incorrectly.

    The second parameter is what you use to retrieve the column – it can be the column name, or the ordinal value. Ordinal value is a number, starting at one (1) based on the columns specified in the SELECT clause. Ordinals aren’t a recommended practice, because if the query changes — if you forget to update the ordinal reference your retrieval of values is screwed. Effectively, the subsequent COUNT query returns a value higher than the number of columns in the first queries SELECT clause.

    $stid = oci_parse($conn, "SELECT Id 
                                FROM test 
                            ORDER BY LENGTH(Id), Id ASC");
    
    oci_execute($stid);
    
    $lastid = oci_result($stid, 1);
    

    oci_result lacks the third parameter that mysql_result provides.

    To make things work in Oracle, use:

    $stid = oci_parse($conn, "SELECT x.id
                                FROM (SELECT Id 
                                        FROM test 
                                    ORDER BY LENGTH(Id) DESC, Id DESC) x
                               WHERE ROWNUM = 1");
    
    oci_execute($stid);
    
    $lastid = oci_result($stid, 1);
    

    The updated query will return one row, the latest based on reversing the ORDER BY you were using previously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this query: SELECT * from likes GROUP BY url ORDER BY
i am adding data from vba excel using adodb into a mysql database everything
This SELECT finds Kelly as expected: select [First Name], [Last Name], Phone from [Data$]
So I'm using this code to get the last entry for each day from
The following code displays only the last record from the database. How to display
I am currently using: SELECT * FROM OptSubjectItems ORDER BY yr, subject; to get
This is my query, CREATE VIEW employee_vu AS( SELECT employee_id,last_name employee,department_id FROM employees); I
How would I select all but the last child using CSS3 selectors? For example,
If I have this select: <select id=days> <option value=0>Today</option> <option value=1>Yesterday</option> <option value=7>Last week</option>
the row source for a listbox looks like this: SELECT users.id, users.first, users.last, chavrusas.luser_type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.