Welcome Guest, Not a member yet? Register   Sign In
What does this active record query do? -> db->select('1', false)
#1

[eluser]pyrokinesis[/eluser]
Hi everyone,

I was wondering what does this active record query do?

Code:
$this->db->select('1', false)

Thanks
#2

[eluser]Frenky.net[/eluser]
This selects "1", the second parameter "FALSE" tells the function to not escape the input.

Code:
SELECT 1

Not very exiting code, but could be userfull in some cases.
#3

[eluser]pyrokinesis[/eluser]
Thanks for the reply Frenky.net,

I still don't get it, there's no column called '1' and it needs to select more than 1 record...

This is the full active record query:
Code:
$this->db->select('1', FALSE);
$this->db->where('ip_address', $ip_address);
$query = $this->db->get('login_attempts');
return $query->num_rows();

and this is the table structure
Code:
mysql> EXPLAIN login_attempts;
+----------------+-------------+------+-----+-------------------+-----------------------------+
| Field          | Type        | Null | Key | Default           | Extra                       |
+----------------+-------------+------+-----+-------------------+-----------------------------+
| id             | int(11)     | NO   | PRI | NULL              | auto_increment              |
| ip_address     | varchar(40) | NO   |     | NULL              |                             |
| account_number | varchar(50) | NO   |     | NULL              |                             |
| time           | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------------+-------------+------+-----+-------------------+-----------------------------+

Any help is very appreciated Smile
#4

[eluser]Phil Sturgeon[/eluser]
Are you confusing select() for limit()?

select() allows you to name the columns you want returned from the database.

limit() allows you to specify how many results you would like back.
#5

[eluser]Frenky.net[/eluser]
Now i see! The reason for this is quite simple. He's trying to get the number of login attempts.
Where others would return just one column, he isn't returning any data from the table.
He's just returning a static number, in this case 1.
This will speed up the selection proces as he isn't accessing a column. So he gets the same result but then quicker!

This is a strange way of doing:

Code:
SELECT COUNT(*) FROM `table` WHERE `column` = "value"

I think the count(*) is quicker, but i don't know for sure.
Hope this helps!
#6

[eluser]Phil Sturgeon[/eluser]
That makes sense. This code will do the trick:

Code:
$num = $this->db
    ->where('ip_address', $ip_address)
    ->count_all_results('login_attempts');
#7

[eluser]pyrokinesis[/eluser]
Thanks for the replies guys,

What I don't get is that if the column '1' doesn't exists in the table no error is returned saying something like 'Column 1 doesn't exist'...

I'm more wondering how it actually works instead of banging out an error?

Thanks Smile
#8

[eluser]Phil Sturgeon[/eluser]
1 is not a valid table name, its an integer so it gets treated like one.

SELECT 1 will return 1 in plain MySQL, but not in AR as its not a full query.
#9

[eluser]pyrokinesis[/eluser]
But this is the query in the profiler:
Code:
SELECT 1
FROM (login_attempts)
WHERE `ip_address` = '100.100.100.100'

so it's looking for a column called 1? I don't get it Sad
#10

[eluser]Phil Sturgeon[/eluser]
The code I have given you works fine.




Theme © iAndrew 2016 - Forum software by © MyBB