Showing posts with label MySql. Show all posts
Showing posts with label MySql. Show all posts

Thursday, 22 September 2011

Removing special characters from Mysql database

I have two ideas to remove extra unnecessary special characters. If we are adding data directly to database through form or some other means and don't have control over the type of data inserted than we can use this simple query right after the mysql_connect( ) call:
 mysql_query("SET NAMES 'utf8'");
And than your insert query such as $query=mysql_query("INSERT into tablename VALUES(value1,value2,...)");


Another way is if data is already stored in database and we want to show data without special characters such as in xml feeds or some other form than we can update the database table with following query:
UPDATE tablename SET fieldname = replace(fieldname,"word_to_replace","with_this_word");


Suppose we have to replace 'he' with 'him' in whole row of a database table than we can use above query as:
UPDATE tablename SET row-name = replace(row-name,"he","him");

Hope this might help you.;)


More information on www.narendrarathore.com

Wednesday, 14 September 2011

How to copy one table content from one database to another database using PHP


This is a simple problem faced by me to get required objective. After hours of googling i come up with some code to achieve my task.
Let's two databases and two corresponding tables, db1.tbl1 and db2.tbl2.

$query=db_query("SELECT * FROM db1.tbl1");
while($row=db_fetch_object($query))
{
$row1=$row->row1;
$row2=$row->row2;
$time=time();
$insert_query=db_query("insert into db2.tbl2 (field1,field2,time) VALUES ('%d','%s','%d')",$row1,$row2,$time);
   }

More information on www.narendrarathore.com