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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:28:57+00:00 2026-05-23T08:28:57+00:00

The following codeigniter function takes a (string) parameter and returns the (integer) ID of

  • 0

The following codeigniter function takes a (string) parameter and returns the (integer) ID of the row. It works fine if i pass string values, but if integer 0 is passed, it returns ID of first row in database. In principle, it should return the user_id only if the the user_name exists in the database. Since there is no user_name called 0, it should return false.
Can someone tell why it is behaving like this and how it can be fixed?

Thanks.

public function get_user_id($user_name)
    {
        $this -> db -> select('user_id');
        $this -> db -> from('users');
        $this -> db -> where('user_name', $user_name);
        $this -> db -> limit(1); 

        $query = $this->db->get();

        if ( $query->num_rows > 0 )
        {
            $row = $query->row();
            return $row->user_id;
        }       
    return false;       
    }

For example:

$user_name = "test";  //works fine, returns id.
$user_name = "0";   //works fine, doesnt return anything
$user_name = 0;     //Problem. returns ID of first row.
  • 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-23T08:28:57+00:00Added an answer on May 23, 2026 at 8:28 am

    You should add a test of the kind if ($user_name == "") return false; to catch this.

    Apparently where with second argument 0 always matches, e.g. translates into SQL WHERE user_name, rather than WHERE user_name = "", and WHERE user_name is short-hand for WHERE user_name != "" — the opposite of what you wanted 🙂

    Running this test at the beginning saves you a full database query when you’re passed an argument that will necessarily result in false.

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

Sidebar

Related Questions

i'm trying to learn codeigniter (following a book) but don't understand why the web
I use the following function in CodeIgniter, to get my latest tweet: function tweet($id)
I've written the following CodeIgniter model function to grab and iterate through a MySQL
In Codeigniter, I have the following model function get_item_history($id) { //from metadata_history get item_id
I have the following php code using the codeigniter framework $this->form_validation->set_rules('money', 'Money', 'integer|required|xss_clean'); It
I have the following code from Codeigniter index.php My understanding is that, If /
I would like for preg_replace_callback to use a library function from CodeIgniter as its
I am using the following routing rule in my codeigniter project's routes.php file: $route['manage/(:any)']
Please see the following controller in CodeIgniter 2 <?php class user extends CI_Controller {
I’m doing the following queries in codeigniter and can’t figure out how to get

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.