简体   繁体   中英

Incorrect row ID returned in simple select query

I am using Codeigntier to run the a query like this:

$query = $this->db->query("SELECT users.*, user_profiles.* FROM users, user_profiles WHERE " . "users.id = $user_id AND user_profiles.user_id = $user_id");
$row = $query->row();

I have placed an echo to get the last query $this->db->last_query() . Which shows:

SELECT users.*, user_profiles.* FROM users, user_profiles WHERE users.id = 6850 AND user_profiles.user_id = 6850

The result it returns is:

object(stdClass)[24]
  public 'id' => string '6849' (length=4)

The id the result shows is 6849 . I nearly fell of my chair! I ran the same query on my MySQL database and the id returned for that exact same query is 6850 . 6850 is the correct ID.

I have no idea how to debug this as this is the simplest of queries. Any help appreciated.

Explicitly select your columns, and AS *something* alias them. Then check the ID's.

It is likely that the id of the table user_profiles is being shown rather than the actual user id from the users table that you expect as you are not specifying the columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM