Welcome folks today in this blog post we will be using php
to copy
and transfer
data from one mysql table
to another in browser. All the full source code of the application is shown below.
Get Started
In order to get started you need to download the xammp control panel
and start the apache
server and mysql
database as shown below
And after that we need to create the database
and inside it we will be creating a sample user
table and insert some data as shown below
And now we need to create the userclone
table inside which we will transfer or copy
all this above data as shown below. The structure needs to be the same in terms of columns
And now make an index.php
script and copy paste the below code
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $host="localhost"; $username="root"; $password=""; $db="dbsample"; $connection= new mysqli($host,$username,$password,$db); if($connection === false){ die("Not connected to database"); } ?> |
As you can see we are first of all connecting
to mysql database using the host
,username,password and dbname
and then we are using the mysqli
constructor to connect and we pass these 4 things to connect to database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $host="localhost"; $username="root"; $password=""; $db="dbsample"; $connection= new mysqli($host,$username,$password,$db); if($connection === false){ die("Not connected to database"); } $sql_query = "INSERT userclone select * FROM user"; if($connection->query($sql_query) === true){ echo "data copied successfully"; } else{ echo "some error occured"; } ?> |
As you can see we are using the INSERT
statement to copy all the data
and the rows from the user
table to the userclone
table and we are displaying the success
messages if the query is successful.