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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:02:23+00:00 2026-05-26T18:02:23+00:00

I’m using CodeIgniter a lot lately, with array methods for querying data: usually result_array()

  • 0

I’m using CodeIgniter a lot lately, with array methods for querying data: usually result_array() or row_array() DB methods. I noticed a mistake that sometimes happens without error notices (old code – not mine – I’m just the bug fixer). It’s the typical ambiguous column name issue that has been posted many times here in StackOverflow.

For example:
PHP & MYSQL: How to resolve ambiguous column names in JOIN operation?

With CodeIgniter, there’s no ambiguous field error message. The array is populated as usual with the field names; ambiguous or not. Is there anyway to prevent this from within CodeIgnitier by displaying or logging an error message?

Does anyone have any ideas on how to log an error message (using CI log_message() perhaps) with PHP when ambiguous fields issues arise?

  • 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-26T18:02:24+00:00Added an answer on May 26, 2026 at 6:02 pm

    This is possible. The reason that CI doesn’t throw up SQL errors about ambiguous column names is because it appends the name of the table when selecting so for example

    SELECT `table`.`name`, `table2`.`name` FROM `table`, `table2` ...
    

    The reason you then don’t get to see these columns is because the array key generated by mysqli_fetch_assoc() or whichever driver you’re using is name for both and obviously gets overwritten. The following answer is suitable for those using CI2.x.

    Extend the Database driver result class in a similar fashion used in my answer to CodeIgniter: SQL Audit of all $this->db->query() method calls?. Once you’ve extended the database driver result class you have two options. You can throw an error as you requested as follows:

    /**
     * Result - associative array
     *
     * Returns the result set as an array
     *
     * @access  private
     * @return  array
     */
    function _fetch_assoc()
    {
        if (!($row = mysql_fetch_array($this->result_id)))
        {
            return null;
        }
    
        $return = array();
        $row_count = mysql_num_fields($this->result_id);
    
        for($i = 0; $i < $row_count; $i++)
        {
            $field = mysql_field_name($this->result_id, $i);
    
            if( array_key_exists( $field, $return ) ) {
                log_message('Error message about ambiguous columns here');
            } else {
                $return[$field] = $row[$i];
            }
        }
    
        return $return;
    
        //Standard CI implementation
        //return mysql_fetch_assoc($this->result_id);
    }
    

    Or alternatively you can further manipulate the output of the function to simulate the query behavior by prepending tablename_ to the column name resulting in an array like

    SELECT * FROM `users` LEFT JOIN `companies` ON `users`.`id` = `companies`.`id`
    Array (
        [users_id] => 1
        [users_name] => User1
        [companies_id] => 1
        [companies_name] => basdasd
        [companies_share_price] => 10.00
    )
    

    To achieve this you can simply modify the for loop in the above function to the following:

    for($i = 0; $i < $row_count; $i++)
    {
        $table = mysql_field_table($this->result_id, $i);
        $field = mysql_field_name($this->result_id, $i);
        $return["$table_$field"] = $row[$i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6

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.