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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:06:19+00:00 2026-05-29T10:06:19+00:00

I know there are duplicates of this question, and the answer was to refactor

  • 0

I know there are duplicates of this question, and the answer was to refactor the code.

However I’m not sure where to begin with this particular function.

I’m practising my OOP API skills in PHP5 and decided to write my own little Database API.

I have a function with 4 nested if statements, Im not even sure if 4 nested if’s are a lot.

But this piece of code just seems messy to me and was wondering if anybody could provide any tips of how to optimise, reduce if’s etc.

How would this kind of function be written in a real-world scenario?

My Code Follows:

public function custom_query( $sql_query_string, $single_column = false){

    $link = $this->_Link_ID;

    // IF LINK IS VALID RESOURCE
    if ( is_resource( $link ) ) {

        $query_resource = mysql_query( $sql_query_string, $link );

        // IF QUERY WAS VALID
        if ( is_resource( $query_resource ) ) {

            $this->_Query_ID = $query_resource;

            $row_count = mysql_num_rows( $query_resource );

            // IF $ROW_COUNT IS A NUMBER, VALID ROWS WERE FOUND
            if ( is_numeric( $row_count ) ) {

                if ( $single_column ){

                    $result_set = mysql_fetch_assoc( $query_resource );
                    return $result_set;
                }

                else {

                    $result_set = array();
                    for ( $row = 0; $row < $row_count; $row++ ) {
                        $result_set[$row] = mysql_fetch_assoc( $query_resource );
                    }
                    return ( object ) array ( 'row_count' => $row_count, 'result_set' => $result_set );
                }
            }

            else {
                die( "Failed To Retrieve Row Count Query: $sql_query_string MySQL Error: " . mysql_error( $link ) );
            }
        }
        else {
            die( "Invalid Query : $query_string. MySql : " . mysql_error( $link ) );
        }
    }
    else {
        die( "Query attempted without valid link resource.  Query : $query_string" );
    }
}

Thanks,

Alex

  • 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-29T10:06:20+00:00Added an answer on May 29, 2026 at 10:06 am

    If you decide to stop using die() in your function you can rewrite your code to form:

    class Yours {
      protected $errno = 0;
      protected $error = '';
    
      public function custom_query( $sql_query_string, $single_column = false){
    
        $link = $this->_Link_ID;
    
        // IF LINK IS VALID RESOURCE
        if ( !is_resource( $link ) ){
           $this->error = "Query attempted without valid link resource.  Query : $query_string";
           $this->errno = -1;
           return null;
        }
        ...
      }
    
    }
    

    Or you can use exceptions;

      public function custom_query( $sql_query_string, $single_column = false){
    
        $link = $this->_Link_ID;
    
        // IF LINK IS VALID RESOURCE
        if ( !is_resource( $link ) ){
           throw new Exception( "Query attempted without valid link resource.  Query : $query_string", -1);
        }
        ...
      }
    

    I personally would go for exceptions… But I guess there are people that disagree with me and I’d like to read their arguments.

    EDIT Exception example. We’ll extend php Extension class:

    class DbException {
      protected $sql;
    
      public __construct( $message = "", $code = 0, $sql = '', $previous = NULL){
        parent::__construct( $message, $code, $previous);
        $this->sql = $sql;
      }
    
      public function getSql(){
        return $this->sql;
      }
    }
    
    // In model:
    throw new DbException( "Query attempted without valid link resource.", -1, $query);
    
    // In main application:
    try {
      ob_start();
      // run whole application
    } catch( DbException &e){
      ob_clean(); // Suppress all output so far
    
      echo "<html><body><div class="error">" . htmlspecialchars( $e->getMessage()) . "</div>";
      if( NOT_IN_PRODUCTION){
        echo "<div class='sql'>" . htmlspecialchars( $e->getSql()) . "</div>";
      }
      echo "</body></html>";
    }
    

    Exceptions:

    • provide backtrace (for easy debugging)
    • can be caught by type (and therefore you can handle them in appropriate place or let them propagate to main application)
    • can contain additional info for better debugging
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this is an extremely obvious question, and that there's a function that
I'm sure there is an answer to this somewhere but I'm clearly using the
I assume that there is probably no satisfactory answer to this question, but I
I know this type of question has been asked before, but I could not
I know this question may be possible duplicate of many others questions but there
I know this could be a duplicated question, but also there are conflicting answers
I want to know is there any way that I can remove duplicates from
I know that The Treeset Sort the input automatically but don't accept duplicates.Is there
I know there is a lot on this topic but I can't get any
Ok, I know this seems like a duplicate question, but don't think it is.

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.