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

  • Home
  • SEARCH
  • 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 8250821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:01:52+00:00 2026-06-08T00:01:52+00:00

Is it possible to re-write the code below, maybe even with an if (result

  • 0

Is it possible to re-write the code below, maybe even with an if (result > 0) statement, in just one line (or simply shorter)?

// a simple query that ALWAYS gets ONE table row as result
$query  = $this->db->query("SELECT id FROM mytable WHERE this = that;");
$result = $query->fetch_object();
$id     = $result->id;

I’ve seen awesome, extremely reduced constructs like Ternary Operators (here and here – btw see the comments for even more reduced lines) putting 4-5 lines in one, so maybe there’s something for single result SQL queries like the above.

  • 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-06-08T00:01:54+00:00Added an answer on June 8, 2026 at 12:01 am

    You could shorten

    $query  = $this->db->query("SELECT id FROM mytable WHERE this = that;");
    $result = $query->fetch_object();
    $id     = $result->id;
    

    to

    $id = $this->db->query("SELECT id FROM mytable WHERE this = that")->fetch_object()->id;
    

    but this, and the original code will emit errors, if any of the functions returns an unexpected response. Better to write:

    $query  = $this->db->query("SELECT id FROM mytable WHERE this = that");
    if (!$query) {
         error_log('query() failed');
         return false;
    }
    $result = $query->fetch_object();
    if (!$result) {
         error_log('fetch_object() failed');
         return false;
    }
    $id     = $result->id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Even though it is possible to write generic code in C using void pointer(generic
I want to know if it is possible to write java code in android
Is it possible to write generic class with one constructor which explicitly defines type
Is it possible to write this query in codeigniter active records? $Main_Nav_Query = mysql_query(SELECT
I'd like to minimize synchronization and write lock-free code when possible in a project
I would like to write a function in Objective-C such as the one below,
Does anybody know if it's possible to write the code in the catch block
Is it possible to write a code segment and call it, instead of writing
I'm trying to write a join statement, but am finding that the query I'm
Is it possible to write code like the following. I'm trying to using Moq

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.