I have a following query
$this->db->set('registerStep', $param)
->where('id = ',$user_id)
->update($this->table_name);
Above Query is producing below sql code. even though I'm supplying only one where condition.
UPDATE `users` SET `registerStep` = 2 WHERE `id` = 33 AND `id` = '165'
I think active record is using some cached where condition, is there any way I can free where condition.
I tried using
$this->db->flush_cache();
But it's not helping.
http://codeigniter.com/user_guide/database/active_record.html#update
->where('id = ',$user_id)
is incorrect.
->where('id',$user_id)
is correct.
Your query is fully correct. But I believe what you used a $this->db->where() before current query. Use the following code and you will see all the previously defined "where" statements:
print_r($this->db->ar_where);
$this->db->set('registerStep', $param)
->where('id = ',$user_id)
->update($this->table_name);
Related
The following is my mysql query:
select * from db_posts LEFT JOIN db_followers ON db_posts_user_id = db_followers_following AND db_followers_user_id = 276
How can I convert it to codeigniter using query builder?
$this->db->where('db_posts_user_id', 'db_followers_following');
$this->db->where('db_followers_user_id', 276);
$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id');
$query2 = $this->db->get('db_posts')->result_array();
I get the required result when I use mysql query. But I get a blank array when I use codeigniter queries. What is wrong in my CI Query?
why are you using those where clause? if you are using it as condition for your 'JOIN' process, try it like this:
$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id and db_followers_user_id = 276', 'left');
$query2 = $this->db->get('db_posts')->result_array();
Points to Note
1)as previous comments said, you should 'Left Join' if you want to get all posts from 'db_posts'.
2) You can add Multiple conditions in ON Clause using and but within ''. all your conditions should be specified before second comma(,) which is before mentioning 'LEFT'.
Hope this will help. Lemme know if this help
Try adding the "Left" option to tell it what kind of join to do:
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id', 'Left');
Also I'm not sure you need
$this->db->where('db_posts_user_id', 'db_followers_following');
this is already covered by your JOIN statement.
Source:
http://www.bsourcecode.com/codeigniter/codeigniter-join-query/#left-join and https://www.codeigniter.com/userguide3/database/query_builder.html
First of all I'm new to the framework. Check the query I'm trying to transcript using the CI database object.
$where = "(".$this->tbl.".invoiceNumber = '".substr($searchFor,3)."'
OR EXISTS (SELECT 1 FROM op_orders_products WHERE idProduct = ".(is_numeric(substr($searchFor,3)) ? substr($searchFor,3) : '-1')."
AND idOrder = ".$this->tbl.".id)
)";
Should I do a separate subquery? Would like to make it all in one.
This is how I started. I wanna make sure the variables are binded and not passed as strings as in the original query.
$this->db->group_start()
->where($this->tbl.".invoiceNumber", substr($searchFor, 3))
->or_group_start()
// I'm missing this EXISTS select subquery
->group_end()
->group_end()
Thanks a lot for the help.
There is no EXISTS equivalent in Codeigniter's Query Builder Class that I'm aware of. So I'd just write it like this:
$this->db->group_start()
->where($where)
->group_end()
see documentation here
binding:
with CodeIgniter you can bind variables to your query like this:
$sql = "SELECT * FROM table WHERE a= ? AND b= ?";
$query = $this->db->query($sql, array($a,$b));
This binds variables $a and $b to the corresponding positions (?) inside the query string.
escaping
$this->db->escape()
this function escapes string data and adds single quotes around it.
I try to update a row in a database, but I can't do that. Here is my sql:
$sql = "UPDATE `voting_nomination_counter`
SET `quantity`=quantity+1
WHERE `nid` = '$nid'
AND nominee = '$nominee'";
I suspect the problem is here - AND nominee = '$nominee'"; because when I remove this from the query all works and updates fine. Help, please.
Try this:
$sql = "UPDATE voting_nomination_counter SET quantity=quantity+1 WHERE nid = '$nid' AND nominee = '$nominee'";
I solve this problem, if I want to update WHERE string = string I just need to use this statement UPDATE table SET field = REPLACE(field, 'string', 'anothervalue') WHERE field LIKE '%string%';, thanks guys!)
#excluded_once Looks like you were able to solve your issue. So in future do not ever use variable names directly into SQL string. Always use db_query or db_select and then always bind the variables into SQL, it will help you prevent from SQL injections and other attacks.
In a worker shift monitoring/accounting system I've built, there is an option to delete Staff Members. This works fine but I would like to archive shifts for staff members who are deleted.
I looked into it and it seemed the best way to do this would be to update my shift table, before the delete, into another table. I looked up how to do this via another Stack Overflow post however I'm getting an error: fatal error call to member function on a non-object.
Based on what I can find, this error is caused when you try to pass a null value, which has left me confused as the value I'm trying to pass is a GET and is working fine when I test it.
$sql = "
UPDATE table_archive
SET table_shift.shift_date = table_archive.shift_date,
table_shift.start_time = table_archive.start_time,
table_shift.end_time = table_archive.end_time,
table_shift.total_hours = table_archive.total_hours,
table_shift.rate_of_pay = table_archive.rate_of_pay,
table_shift.uniqueid = table_archive.uniqueid,
table_shift.addedBy = table_archive.addedBy,
table_shift.paidRate = table_archive.paidRate,
table_shift.totalPaid = table_archive.totalPaid
FROM table_shift, table_archive
WHERE table_shift.uniqueid = ?
";
$stmt = $connection->prepare($sql);
$deleteid = htmlentities($_GET['id']);
$stmt->bind_param('s', $deleteid);
$stmt->execute();
I'm stuck as to why this wont pass, the GET cant be a null value as the test delete I'm using at the moment passes the same variable and works fine. mysqli_query($connection,"DELETE FROM table_staff WHERE uniqueid='$deleteid'")
It may be that I'm using the SQL code wrongly or there is some silly thing I've forgotten but this has me stumped. Failing to fix this code, any other suggestions as to how to achieve the intended function are welcome.
You can't UPDATE FROM. Your syntax is wrong.
Instead, use this:
INSERT INTO table_archive
SELECT * FROM table_shift WHERE table_shift.uniqueid = ?
Is the use of bindParam correct?
If you use a ? it should look like this:
SELECT ...
WHERE column_name = ?
$sth->bindParam(1, $value_in_php, PDO::PARAM_INT);
If it's not ? but :param_name use this:
SELECT ...
WHERE column_name = :param
$sth->bindParam(':param', $value_in_php, PDO::PARAM_INT);
Your error sounds not like an SQL error, but a PHP error.
And if you want to update the table_archive table the SQL doesn't look correct. It should imho be like this:
UPDATE table_archive
SET table_archive.shift_date = table_shift.shift_date
, to_table_that_should_be_updated = from_table_with_value_to_update
FROM table_shift
WHERE table_shift.uniqueid = ?
How would i get the rowID for an update query (i am of course not updating by rowID)
The below code is what i am using (as a public function within a Class), but of course using "lastInsertId()" is not working for me (i didnt really expect it to work and it returns 0)
$query = "UPDATE tSecurityDepositPaymentAddresses
SET
tGuardians_GuardianID = ?
WHERE tGuardians_GuardianID = 0
LIMIT 1";
$stmt = $db->prepare($query);
$stmt->execute(array($GuardianID));
return $db->lastInsertId();
Could i combine the update query within a nested select? or is there a simpler way?
sorry if my question sounds bumb, but im still learning...
Thanks