Blog Details

concate field data using update query in php mysql


                                
                                    <strong>UPDATE tableName SET fieldName = CONCAT(fieldName, 'New Data To Add Here') WHERE ...</strong>
<p style="text-align: left;"><em>example :</em></p>
     <strong>UPDATE ws_contact SET webpage = CONCAT(webpage, 'www.wamasoftware.com') WHERE id=1</strong>

But this query will not work if the field is NULL (which makes sense as there is nothing to append),  you can use below query to solve NULL problem

<strong>UPDATE <strong>tableName</strong> SET field = CONCAT(COALESCE(field, ''), 'New Data') WHERE ...</strong>

<em>example :</em>

<strong>     UPDATE ws_contact SET webpage = CONCAT(COALESCE(webpage, ''), 'www.wamasoftware.com') WHERE id=1</strong>

Basically this check whether the field is NULL or not. If it's NULL then it will automatically use blank data for the field otherwise it'll default to the field's value. Then, it will do the concatenation of the new data.
<div> You could also use IFNULL() instead of COALESCE()</div>
                                    


Categories

Php mysql

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote