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 7022821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:37:53+00:00 2026-05-27T23:37:53+00:00

I am going through my code and I am running so many queries it

  • 0

I am going through my code and I am running so many queries it is getting long. If I just want to store a single variable with a value, I have to do this:

switch($type) {
    case "next":
    if ($stmt = $mysqli->prepare("SELECT sort_order FROM user_slides WHERE user_id = ? AND sort_order > ? ORDER BY sort_order LIMIT 1")) {
        $stmt->bind_param('is', $user_id, $sortId);
        $stmt->execute();
        $stmt->bind_result($next_sort_id);
        $stmt->store_result();
        $stmt->fetch();

        return $next_sort_id;

        $stmt->close();
    }
    break;

    case "first":
    if ($stmt = $mysqli->prepare("SELECT MIN(sort_order) as max_slides FROM user_slides WHERE user_id = ?")) {
        $stmt->bind_param('i', $user_id);
        $stmt->execute();
        $stmt->bind_result($first_sort_id);
        $stmt->store_result();
        $stmt->fetch();

        return $first_sort_id;

        $stmt->close();
    }
    break;

    case "last":
    if ($stmt = $mysqli->prepare("SELECT MAX(sort_order) as max_slides FROM user_slides WHERE user_id = ?")) {
        $stmt->bind_param('i', $user_id);
        $stmt->execute();
        $stmt->bind_result($last_sort_id);
        $stmt->store_result();
        $stmt->fetch();

        return $last_sort_id;

        $stmt->close();
    }
    break;
}

There has to be an easier way to do this. Where I can just do it like this:

$first_sort_id = QUERY HERE
$last_sort_id = QUERY HERE

So it doesn’t have to be so long, it can be nice and short.

Does anyone know what I need to do to accomplish that?

  • 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-27T23:37:54+00:00Added an answer on May 27, 2026 at 11:37 pm

    First

    If you use return the statement $stmt->close(); will be never executed.

    Second

    Get rid of two vars, you just need one. Don’t get fooled by semantic meanings.

    Third

    A simple reorganization could be this:

    switch($type)
    {
       case "next":
          $Query = "SELECT sort_order";
       break;
       case "first":
          $Query = "SELECT MIN(sort_order)";
       break;
       case "last":
          $Query = "SELECT MAX(sort_order)";
       break;
    }
    
    if ($stmt = $mysqli->prepare("$Query as max_slides FROM user_slides WHERE user_id = ?"))
    {
          $stmt->bind_param('i', $user_id);
          $stmt->execute();
          $stmt->bind_result($value);
          $stmt->store_result();
          $stmt->fetch();
          $stmt->close();
    
          return $value;
    }
    else
       return NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just going through the sample Scala code on Scala website, but encountered an annoying
I have been going through the core jQuery code and had a few why
I have just installed the Windows SDK v7.1 (MSVC 10.0) and running my code
I was going through some code and came across a scenario where my combobox
I was going through some code that I downloaded off the internet ( Got
So I'm going through old code (2.0) and I came across this: object isReviewingValue
I'm going through the source code of the less unix tool by Mark Nudelman,
I'm going through a Fortran code, and one bit has me a little puzzled.
I am going through some example assembly code for 16-bit real mode. I've come
I'm going through some old C#.NET code in an ASP.NET application making sure that

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.