I have sql query for delete multiple record from database
$sql="delete from users where id IN(4,5,6..)";
How to run this query in codeigniter
Per the documents, you can use the query builder, or you can run a direct query on the database.
The query() function returns a database result object when “read” type
queries are run, which you can use to show your results. When “write”
type queries are run it simply returns TRUE or FALSE depending on
success or failure. When retrieving data you will typically assign the
query to your own variable, like this:
$query = $this->db->query('delete from users where id IN(4,5,6..)');
Or you can use the query builder class which allows you to do the following:
$this->db->where_in('id', array(4,5,6));
$this->db->delete('users');
Create a function in you model
function rowDelete($ids)
{
$this->db->where_in('id', $ids);
$this->db->delete('testimonials');
}
And call that in your controller
$this->your_model->rowDelete($ids) // $ids = array(4,5,6);
You can also execute query like this
$delete = "id IN('4','5','6')";
$this->db->where($delete);
$this->db->delete("user");
In this you can also execure your core query.
Related
Using this:
profile = User::find($user)->with('profile')->first();
Will return a query of SELECT * FROM profiles, instead of selecting the profile belonging to the id of the found user.
My user model is as follows:
public function profile() {
return $this->hasOne('App\Models\UserProfile', 'user_id');
}
Why is it not adding a where condition?
What find($id) really does is expand your query, like this:
// from
User::find($user)
// to
User::where('id', $user)->take(1)->get('*')->first()
In other words: it creates the query, limits it to the first row, gets that row - which returns a collection - and then returns the first item in that collection.
That means your with('profile') gets attached to the object, not to the query builder, and then you tack another first() call to that.
What you should do instead is
User::with('profile')->find($user);
... which I assume works but I haven't tested it. :)
I'm trying to call a stored procedure (from mysql) with codeIgniter (3.0.4).
My issue is that I have always the message "Commands out of sync; you can't run this command now".
Here is my code
$this->db->trans_start();
$sql = "CALL setup_resource('{$p1}','{$p2}','{$p3}',#id)";
$this->db->query($sql);
$query = $this->db->query("SELECT #id as my_id");
$this->db->trans_complete();
$my_id = 0;
foreach ($query->result_array() as $row) $my_id = $row['my_id'];
I'm trying to execute with SP and with #id retreive the id returned by my stored procedure => procedure seems that I cannot call another query in the same transaction.
I saw in many topic that CI does not handle correctly multiples query string, and advise tu use "next_results()" function but the result is the same (or I have a message "none next_result() you have to use more_results()").
Also saw that i should use multi_query function from mysqli but did not saw examples of it.
Did anyone have this issue ? What would be the workaround for codeIgniter to handle it?
Thanks for your help ;)
I am encountering a strange issue in Laravel.
The below is an index function in one of my controllers.
public function index($merchant_url_text)
{
//
$deals = DB::table('tbl_deal')
-> join ('tbl_merchant', 'tbl_deal.merchant_id', '=', 'tbl_merchant.merchant_id')
-> where ('merchant_url_text', $merchant_url_text) -> toSql();
//return $merchant_url_text.$deal_id;
dd($deals);
//return $merchant_url_text;
}
As you can see I am passing merchant_url_text from route.
Route::get('/testroute/{merchant_url_text}', ['uses' =>'dealsVisibleController#index']);
When I am trying to debug the query by printing it, I am getting
"select * from `tbl_deal` inner join `tbl_merchant` on `tbl_deal`.`merchant_id` = `tbl_merchant`.`merchant_id` where `merchant_url_text` = ?"
This means that the query builder is not reading the $merchant_url_text variable. However, when I return just that variable, it is being printed.
Just can't figure out why the query builder is not able to include the $merchant_url_text variable in the query when it is available in the index function.
Any suggestions.
I am pretty sure that your code is correct. The SQL output function toSql() does not show the values of variables and only prints out a ? for security reasons.
You may access all your queries by using
$queries = DB::getQueryLog();
It is also printing the query parameters as array.
To get the last query:
dd(end($queries));
To disable the log:
DB::connection()->disableQueryLog();
See the docs for further information.
I am new to laravel so can anybody explain me that what exactly get() function of eloquent ORM does. I have following query and I need to know on which line the database is queried.
$propertiesQuery = Property::with('country', 'city','bannerInvoicesCount','rejectedBannerInvoicesCount')
->where('is_deleted','=',1);
if(Auth::user()->type == 'po') {
$propertiesQuery->where('user_id', '=', Auth::user()->id);
}
$propertiesQuery->orderBy('created_at', 'DESC');
$properties = $propertiesQuery->get();
Now in above code I have accessed Model once whether that is the point when db is queried or when I have called get() function. I am not been able to comprehend the internal working of this. That exactly on which statement the query is executed.
#Raza before eloquant $propertiesQuery->get() step, it prepare your query like:
select * from table where user_id=1
and when you call/trigger the $propertiesQuery->get() method, it forward your query to database and fetch the records against your query. like, normally we do in plain sql:
eg: mysql_query($query);
then mysql_fetch_assoc() etc methods
so get() method contains on: execute the query and return all the records in the array format.
I hope it will help you.
I have a Postgres database with a table for orders and a table order_contents, which includes all items of the order, and the order_id. I'm trying to make a view with order history.
In my model I have two functions, one to retrieve orders, and another to retrieve all the contents of an order, like so:
function retrieve_orders($userID){
$query = $this->db->get('orders');
$this->db->where('user_id', $userID);
return $query->result_array();
}
function get_order_contents($orderID){
$query = $this->db->get('order_contents');
$this->db->where('order_id', $orderID);
return $query->result_array();
}
So in my controller I call retrieve_orders() and pass the resulting array to the view. In the view, I want to create an HTML table for each order with all it's contents. So I iterate through each order and call get_order_contents in the loop.
Everything should work, however what happens is this error shows up in the source:
Error Number: ERROR: column "user_id" does not exist
LINE 3: WHERE "user_id" = '3'
^SELECT *
FROM "order_contents"
WHERE "user_id" = '3'
As you can see, for some reason, when I call get_order_contents() in the view, it uses the where clause I specified for the retrieve_orders() function.
What I have done so far is try to manually stop caching, but to no avail. Any help here is appreciated.
In each function, try to put where() before get().
When you try to get() order_contents it will use where clause from previous where().
Common practice is, use get() at the last call of $this->db after other calls such as where(), etc. See the Active Record documentation in codeigniter.
Chain your db functions like this
return $this->db->where('user_id', $userID)
->get('orders')
->result_array();
why not use $this->db->get_where()