How to update the column with increment value [duplicate] - php

This question already has answers here:
Update the value of a field in database by 1 using codeigniter
(3 answers)
Closed 21 days ago.
I have used this query to increment my column with the selected number like
$this->db->where('my Condition');
$this->db->update('my Table',array('name'=>'gautam','count'=>'2'));
Here I want to add 2 to the actual column value of count. But I can't able to do it with update function.And I cont able to do like
$this->db->update('my Table',array('name'=>'gautam','count'=>'count+2'));
because I'm only getting count of "2" and if I add in my query it is adding ' in my query like
enter code here
Can anyone help me to find out the solution for it.

$this->db->set('name', 'gautam');
$this->db->set('count', 'count+2',FALSE);
$this->db->where('my Condition');
$this->db->update('my Table');
$this->db->set() in Codeigniter

You can use Codeigniter set function to set the value of count with the increment .
From the doc
set() will also accept an optional third parameter ($escape), that
will prevent data from being escaped if set to FALSE. To illustrate
the difference, here is set() used both with and without the escape
parameter.
So you can do something like this :
$this->db->where('my Condition');
$this->db->set('count','count+2',FALSE);//SET COUNT WITH COUNT+2
$this->db->set('name','gautam');
$this->db->update('my Table');

Try this :
$this->db->where('my Condition');
$this->db->set(array('count' => 'count+2', 'name' => 'gautam'), FALSE);
$this->db->update('my Table');

Related

How to make a multiple update on mysql with string replace with value of another column

Hello i'm new in mysql and i have to run a multiple update on my table.
I have 700 records in the table and i have to update them all this way:
table example :
store_id: 1
store_email: storename#gmail.com
for single update i use
UPDATE stores SET email = '1#gmail.com' WHERE id = 1;
i need to update all the emails and replace their name with their id, so it would be like this:
storename#gmail.com --> 1#gmail.com
storename#gmail.com --> 2#gmail.com
storename#gmail.com --> 3#gmail.com
those numers have to be the ID for each store.
Hope you can understand
Thanks for help.
P.S. i need to run it on magento 2
you can use CONCAT() and RIGHT() function for manipulating strings like this:
UPDATE stores SET email = CONCAT(id, RIGHT(email, 9));
The RIGHT('string', n) function extracts n characters (storemail = 9 chars in your case) from a string (starting from right).
Since you are adding id to String column gmail, you can use contact() fucntion like below :
UPDATE stors SET email=CONCAT(id, "#gmail.com") where id=2;

The query is using a variable but i dont know why, can you help me? [duplicate]

This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
What does this code do? Image to show code
$q57 = mysqli_query($Link, 'select count(*) Registos from t57 where c18="1"') ;
$t57 = mysqli_fetch_array($q57) ;
if($t57['Registos'] == 0) {
what i don't understand is that variable "Registos" in front of the count. Is it doing what there?
That is simply an alias, to help you access the result of count(*).
Otherwise that would become the column name in the result set - remove Registos from the query, and then do a var_dump($t57);, you’ll see what I mean.
The as keyword between the two is optional; select count(*) as Registos would be the “long” way of writing this.
https://www.mysqltutorial.org/mysql-alias/

Faster way to fetch a single data from db with codeigniter [duplicate]

This question already has answers here:
getting the value of the single field output using the codeigniter active record
(5 answers)
Closed 2 years ago.
hi want to get only one column from one result from my database. So i do it in this way
$this->db->select('myclolumn')->where('id',$id);
$query = $this->db->get('mytable');
$temp=$query->row_array();
$finalresult=$temp['mycolumn'];
Maybe there is a easier or faster way to do the same?
so based on DanishAli's comment
$this->db->select('myclolumn')->where('id',$id);
$query = $this->db->get('mytable');
$finalresult=$query->row()->mycloumn;
You can do it like this:
$finalresult = $this->db->select('myclolumn')
->where('id',$id)
->get('mytable')->row()
->mycolumn;
All in only one command instead of a lot of variables and extra code.
In case you need to provide a default value if the row is null, break the command to:
$row = $this->db->select('myclolumn')
->where('id',$id)
->get('mytable')
->row();
$finalresult = ($row)?$row->mycolumn:'your default value';

PHP - How to update table using auto increment (value++) [duplicate]

This question already has answers here:
Increment value in MySQL update query
(10 answers)
Closed 6 years ago.
someone can tell me how to update the value from 0 to 1, 52 to 53 (value++) using php script?
i want to search it on go*gle but i dunno the keyword to find it
If you want to update one column, for example 5_sagnat_baik - just run the query:
UPDATE `question` SET `5_sagnat_baik` = `5_sagnat_baik` + 1
You may update your auto-increment column (known as 'id') using a "update" sql in your php script using something like:
'UPDATE table_name SET id=(id + 1), [column2]=[value2], ...'
OR with PHP computed values (here $current_value):
'UPDATE table_name SET id=' . $current_value + 1 ', [column2]=[value2], ...'
The SQL update request is explained here: http://www.w3schools.com/SQl/sql_update.asp (or here: https://mariadb.com/kb/en/mariadb/update/#syntax)
"UPDATE table_name
SET column_name = column_name+ 1
WHERE condition";
Hope this help

MySQL Query - replace or remove 0 (zero) in a column, but not remove the digit zero in a word or number?

I have a table where one column has 0 for a value. The problem is that my page that fetches this data show's the 0.
I'd like to remove the 0 value, but only if it's a single 0. And not remove the 0 if it's in a word or a numeric number like 10, 1990, 2006, and etc.
I'd like to see if you guys can offer a SQL Query that would do that?
I was thinking of using the following query, but I think it will remove any 0 within a word or numeric data.
update phpbb_tree set member_born = replace(member_born, '0', '')
Hopefully you guys can suggest another method? Thanks in advance...
After discussed at the comments you have said that you want to not show 0 values when you fetching the data. The solution is simple and should be like this.
lets supposed that you have make your query and fetch the data with a $row variable.
if($row['born_year'] == '0'){
$born_year = "";
} else {
$born_year = $row['born_year'];
}
Another solution is by filtering the query from the begging
select * from table where born_year !='0';
update
if you want to remove all the 0 values from your tables you can do it in this way. Consider making a backup before.
update table set column='' where column='0';
if the value is int change column='0' to column=0

Categories