get_compiled_select repeating prefix!
When I do $this->db->select('tablename.column').get_compiled_select('tablename', FALSE); $rows = $this->db->get()->result_array();
Fires an error as table prefix is repeated
SELECT prefix_tablenameprefix_tablename.column FROM tablename
This is not valid CI code, and it is unclear even what you'd be trying to do with it:
$this->db->select('tablename.column').get_compiled_select('tablename', FALSE);
And then this ...
SELECT prefix_tablenameprefix_tablename.column FROM tablename
It's not the prefix repeated, it's the prefixed tablename repeated in the SELECT portion of a query, while it is not at all prefixed in the FROM clause (and that looks really questionable to be true).
Please elaborate and be clear on what the issue is.
@narfbg, in user-guide https://www.codeigniter.com/user_guide/database/query_builder.html#id12
The example with database.php('dbprefix' => 'prefix_';), then prefix will be repeated as mentioned!
The linked user-guide page doesn't even contain 'dbprefix' or prefix_ within it ...
@narfbg assume we set ( $db['default']['dbprefix']='ca_'; ) in /application/config/database.php. table [users] columns are ['first_name', 'last_name']. do the following:
$sql = $this->db
->select('users.first_name', 'users.last_name')
->get_compiled_select('users', FALSE);
$rows = $this->db->get()->result_array();
[as in the example I mentioned above, you will get the following database error]
Error Number: 1054
Unknown column 'ca_ca_users.first_name' in 'field list'
SELECT ca_``ca_users.first_nameFROMca_users`
Trying to reproduce any sort of problem on the develop branch, I set 'dbprefix' => 'prefix_' in application/config/database.php.
Then in a controller, I did
$this->load->database();
echo $this->db->select('tablename.column')->get_compiled_select('tablename', FALSE);
This produces
SELECT "prefix_tablename"."column" FROM "prefix_tablename"
which seems fine.
....
Just saw your new reply. The method signature for select is:
public function select($select = '*', $escape = NULL)
You need to pass an array of column names as the first parameter and probably don't want to pass a second parameter at all.
echo $this->db
->select(array('tablename.column', 'tablename.col2'))
->get_compiled_select('tablename', FALSE);
SELECT "prefix_tablename"."column", "prefix_tablename"."col2" FROM "prefix_tablename"
I don't see a bug here.
@zploskey
awesome, now do as in the example
$rows = $this->db->get()->result_array();
Fix your call to select like I showed and it should work. There's nothing wrong with the query that got generated as long as you pass an array to select.
@zploskey I already tried that, it throw an error as mentioned.
Please post exactly the line of code, exactly the query generated, and exactly the error produced, formatted as code so that we can actually read them.
Thanks @zploskey Just for the record, I am using CI 3.1.5. considering 'dbprefix' is 'ca_' ! Home controller
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$sql = $this->db
->select(array('users.first_name', 'users.last_name'))
->get_compiled_select('users', FALSE);
echo $sql; // output is perfect
$rows = $this->db->get()->result_array(); // Error code 1054 [shown in attachment]
die(var_export($rows));
}
}

Thanks. We can work with this.
For the record, the query shown in the error is:
SELECT `ca_``ca_users`.`first_name`, `ca_``ca_users`.`last_name` FROM `ca_users`
Based on the syntax I assume this is on MySQL.
In the future please post text as text. Images tend to become unavailable after a while.
What happens if you do this?
->select(array('first_name', 'last_name'))
or this
->select('table_name.first_name, table_name.last_name')
or even just this
->select('first_name, last_name')
I couldn't find any examples in the docs using the dot notation like you were doing. They all either pass a string to select or an array of only the field names. It's possible it's not supported to do it the way you're doing it. There are some tests that use the second form above, so that should work.
Thanks @zploskey, in fact, I made it as simple, but with [ ->join() ] then [table_name] is required for column select as SQL goes! And yes, I am beginner and I am using MySQL.
Doing some experimenting, I've found out a couple of things.
select()does not generally accept an array. You should give it a string. It just so happens to work for simple arrays of field names, but won't work forarray('table.field1', 'table.field2')etc. So don't rely on that behavior. Makes you wish you were working in a statically typed language. Please forgive my earlier suggestion to do this--I don't actually use the query builder in production so I'm a little unfamiliar with it.- Queries like this with
tablename.fieldnamewill fail unless you supply afrom('tablename'),get('tablename'), or maybe if you have table alias set in the model class (I didn't really look into this).
This seems to work OK regardless of whether you set the dbprefix or not:
$this->db->dbprefix('prefix_');
$rows = $this->db->select('table_name.first_name, table_name.last_name')
->get('table_name')
->result_array();
So anyways, that should be how to do it. You were getting a different query from get_compiled_select() because you were supplying the table name in that call.
I'm pretty sure this is not a bug, but it does seem like some sort of error message would be appropriate if a developer omits get('table_name') or from('table_name') or we otherwise don't know the table name. For example I get this on sqlite:
$rows = $this->db->select('table_name.first_name')->get()->result_array();
// Gives: SQLite3::query(): Unable to prepare statement: 1, no such column: table_name.first_name
which is a bit misleading. It does exist, but the table name isn't set on the query.
So I guess there is some question of whether we should infer the table name from the select if it was of the form table_name.field_name, but I think it might be most appropriate to just throw an exception saying you need to supply the table name. I could also see leaving it as is but maybe spelling this out a little in the documentation.
@zploskey Thanks for your help on this.
However, the following (that you said) is patently false:
select()does not generally accept an array. You should give it a string. It just so happens to work for simple arrays of field names, but won't work forarray('table.field1', 'table.field2')etc.
The very first thing that select() does is explode(',', $string_input) if the input is a string. Everything is stored as an array internally, and as strings are inherently unreliable, we shouldn't encourage their use.
Arrays are more than fine. It's just that there are way too many lazy developers that would riot if we disabled the string option.
The issue here had nothing to do with this either. See the referenced commit.
Interesting. The reason I thought this was not supported was that the documentation for select() says it only accepts a string.
https://github.com/bcit-ci/CodeIgniter/blob/1db998348e8850db79c4afa2d057fa31fb6bc12c/system/database/DB_query_builder.php#L276-L282
Anyways, thanks for fixing the problem here.