How can I fetch data from one database on a server and store them in other database on another server using PHP?

Many times we need to transfer data from one database exists on a server to another database exists on another server ,in that case we can use fourth parameter of mysql_connect function which is an optional parameter. If fourth parameter is true then it will create a new link for another database connection even if we have created database connection earlier with same detail with same database or with another database. Lets discuss how it works by an example.
Function syntax:
mysql_connect ( [string $server [, string $username [, string $password [, bool $new_link [, int $client_flags]]]]] )

Now Connect database1 using host, username and password as given below
$db1 = mysql_connect(”host”,”user”,”pwd”)
mysql_select_db(’db1name’, $db1);
Fire a query on db1
$res1 = mysql_query(”query”,$db1);

Connect database2 using host, username and password and include fourth parameter true as given below
$db2 = mysql_connect(”host”,”user”,”pwd”, true)
mysql_select_db(’db2name’, $db2);
Fire a query on db2
$res2 = mysql_query(”query”,$db2);

Now both link will remain live.If you will not use fourth parameter than setting of $db1 will be overwrite by $db2 so you can not use both objects without using this fourth parameter as true.Now after using fourth parameter you can use both database1 and database2 on same page.You can fetch data from first database and than insert that data in second database .

MySQL Left Outer Join

Joins are very interesting topic in mysql.In my 4 years exp. i feel that every body knows about joins (experts and freshers) but freshers use joins very few.This may be because of their low thinking capability in starting or may be they find joins more typical then their local logic.Maximum beginners try to put their own logic rather then joins.

Today we will talk about left outer join .Not very common join but very effective join.
Suppose you have 2 tables. First “table1″ with 2 fields ‘id’ and ‘name.’ . Second “table2″ with 2 fields ‘id’ and ’salary’.In table1 “id” is primary key and in table2 field “id” is foreign key.

Read the rest of this entry »

How to insert data from one table to another table in mysql

To Insert field values from one table to another in mysql use this simple query

INSERT INTO tablename1 (field1,field2,….,fieldn) SELECT field1,field2,….,fieldn FROM tablename2 WHERE condition

concat function in mysql

CONCAT function in mysql is used to concat two strings.

For ex. CONCAT(’firstname’,’ lastname’);

This functoin will concate this two word and create a single word.

Uses:

SELECT concat(’firstname’,'  ‘, ‘lastname’) AS name FROM tablename .

you can concat many words as you want.In the example above we also concated a space between firstname and lastname to seprate them

Different text fields in Mysql and their field size

In mysql there are three type of text fields with different length

1. Text  = 65000 Bytes.

2. Medium text = 16 million bytes.

3. Long text = 4 trilian bytes.