Friday, December 22, 2017

Duplicate / Copy rows/ records in the same MySQL table

I need to copy/duplicate a lot of rows. If you just export and import sql file it will not work as the table has auto increment id column. So I find an easy solution for my problem. I would like to duplicate a record in a table, but of course, the unique primary key needs to be updated.
First it need to create a temporary table and then update all id to null. then insert into table.

Here is an example query

CREATE TEMPORARY TABLE tmp SELECT * FROM sms_history WHERE date='2017-12-22';
UPDATE tmp SET id=NULL;
INSERT INTO sms_history SELECT * FROM tmp WHERE date='2017-12-22';
DROP TABLE tmp;

This is the most easy and fastest solution for this kind of problem.

Sunday, October 1, 2017

Store php variable in mysql / dynamic email, SMS template

This is a good tutorial if we want to create dynamic Email Template/ Dynamic SMS Template or dynamic content using php mysql. Storing php variable in content and save to mysql database. when fetching data from database replace variable with value dynamically.

 First create a table as your require.  Suppose we want to store this kind of sms template in datasbse

 <p>  
 Hello : [FULL_NAME] <br/>   
 username: [USER_NAME] <br/>   
 email : [USER_EMAIL] <br/>  
 website : [SITE_URL]  
 </p>  

Now we can fetch this data from database and assign in a php variable

 $content = $row['content'];  
 //replace template var with value  
 $token = array(  
   'FULL_NAME' => 'Sharif Ahmed',  
   'USER_NAME' => 'winsharif',  
   'SITE_URL' => 'http://www.esteemcorporation.com',  
   'USER_EMAIL'=> 'winsharif@test.com'  
 );  
 $pattern = '[%s]';  
 foreach($token as $key=>$val){  
   $repVar[sprintf($pattern,$key)] = $val;  
 }  
 $SMSContent = strtr($content,$repVar);  
 echo $SMSContent;  

/////////////////////////////////////Output will be //////////////////////
Hello : Sharif Ahmed 
username: winsharif
email : http://www.esteemcorporation.com 
website :  winsharif@test.com

Total Pageviews