CodeIgniter Database query limit - codeigniter

I'm doing some db querys using ActiveRecord and i need to paginate the results so i do a
$this->db->limit($pPagination['Start'], $pPagination['Length']);
but i don't get any results. Using $this->db->last_query(); it seems that CodeIgniter produces the following SQL
SELECT *
FROM (`viw_contacts`)
WHERE `user_id` = '1'
ORDER BY `contact_name` asc
LIMIT 0 OFFSET 15
which when i run it inside PHPMyAdmin also returns 0 rows.
But if i modify it an run :
SELECT *
FROM (`viw_contacts`)
WHERE `user_id` = '1'
ORDER BY `contact_name` asc
LIMIT 0, 15
Then i get the correct results. Any ideea why CodeIgniter generates this SQL and why it doesn't work?
I use CodeIgniter 1.7.3 and MySQL 5.1.41

Ok, found the issue.
It's
$this->db->limit($pPagination['Length'], $pPagination['Start']);
instead of
$this->db->limit($pPagination['Start'], $pPagination['Length']);
First param is the Length and second is the offset, not the other way around as i thought.

Related

Laravel query order the inrandormorder query

I have 40 rows in my table, what i want is to get 20 row randomly, and after order these 5 list by id; these 2 in one query;
for example in range of id 1 to 40; i get from the random order 15 10 14 9 3;so in the final result i expect to get 3 9 10 14 15
i tried to use inRandomOrder and orderBy in same time
$query->inRandomOrder()->orderBy('id', 'asc')
but the inRandomOrder query always has the final execution of my query; and if i switch their position:
$query->orderBy('id', 'asc')->inRandomOrder()
the orderBy always has the final execution
2 solutions I guess, if you manipulate only dozens of rows, you can use Laravel collection to order by ID your query result.
Just use:
$result = $query->inRandomOrder()->get();
$resultSortedById = $result->sortBy('id');
Or using SQL query, you can use this query:
SELECT * FROM
(
SELECT * FROM table
ORDER BY RAND()
LIMIT 20
) result
ORDER BY id ASC
You can translate in Eloquent way, or as SQL directly.
I am not sure to understand your question btw: order these 5 list by id ? What do you mean exactly ?

How to use Nested functions substr(), cast() and Max() in query builder in laravel 8?

I posted this question yesterday but I think it's unclear so I deleted it and posted it again with more details.
In my oracle database I have a USERS table with id_user defined as varchar and this varchar is like this: '145/1' ...... '145/9' so to add a new user I check the maximum value ('145/9') and add 1 to the second part of id_user (after the slash) so that the id_user is '145/10'.
The steps are like this:
First: I'm using substr() to get the second part (after the slash) of all id_user.
Second: I use Cast() to convert it to Int.
Third: I use Max() to get the maximum value of Int numbers.
Finally in my laravel code I use the result of this query(the result is 9) and add 1 to it and insert a new user in the users table with id_user = '145/10' and so on.
This query works fine but I need it in the Query Builder so I am trying a lot of queries but they didn't work.(please help me)
SELECT MAX(CAST(SUBSTR(id_user, INSTR (id_user, '/') + 1) AS INT)) AS Aggregate
FROM "users"
WHERE "ID_USER" LIKE '145/%';
Finally, this query gives me the correct maximum:
DB::table('users')->select(DB::raw('MAX(CAST(SUBSTR(id_user,INSTR(id_user, \'/\') + 1) AS INT)) as max')) ->where('id_user','like','145'.'/%')->get()[0];

How to Eager Load The Session Query in laravel?

I am trying to figure out where laravel makes the session query, i have these queries when i just log in my app
/****************************** Database Queries ******************************/
0.79 | select * from `users` where `users`.`id` = ? limit 1 | 1
0.8 | select `roles`.*, `role_user`.`user_id` as `pivot_user_id`............
Queries: 2
Time: 1.59
/****************************** End Queries ***********************************/
I would like to eager load the first query with the second query, i use the second query in every pages of my website to get the user nav bar according with his role
if i do Auth::user()->roles()->id the second query would run.. but if i do User::with('Role')->find(1); for example i would get the same result as the first query plus+ the second.. am i right? how could i change this laravel behavior to runs trough all the pages.. i would save one query :)
hope i could explain myself clearly..

laravel function ::with is not generating the correct sql

I have the following query in Laravel 4:
Tasks::with('tasks_status')
and generate the following sqls:
select * from `tasks`
select * from `tasks_status` where `tasks_status`.`status` in (2, 0)
The connection in models is well done.
The "tasks_status" table have values from 1 to 7 (as id's).
The "tasks" table return values for statuses from 1 to 7.
The second sql shouldn't be?
select * from tasks_status where tasks_status.status in (1,2,3,4,5,6,7)
Thank you.
The problem is solved. I made the incorrect keys relations.
Thank you for your time.

sql query - variables - date - visual studio 210

Trying to search between two dates to only get certain rows. A 7 day search.
#date = DateTime.Today
#date2 = //need it to be the prior 7 days
SelectCommand = "SELECT [DateReceived], [DeviceLevel] FROM [TBLReadings] WHERE [DateReceived=#date] <= [DateReceived=#date2] ORDER BY [DateReceived] DESC;
This is wrong but I hope it explain what I am trying to do - only ever used PHP and MySQL.
if you are trying to search between 2 dates, you can use between
the query will be something like
select [dateRecieved], [DeviceLevel] from YourTable where DateReceived between #date and #date2 ORDER BY [DateReceived] DESC
in c#, to get 7 days you can use Datetime.Now.AddDays(7)
http://msdn.microsoft.com/en-us/library/system.datetime.adddays.aspx
sorry for the bad english!
edit: that's only the query, use it as a string in the select command

Resources