Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insert data from Firebird to mysql

Posted on 2013-11-04
10
Medium Priority
?
1,032 Views
Last Modified: 2013-11-05
Hi,

I have been set a task of inserting customers from a table within our firebird database in to a table within our mysql database.

So far all I have done is created a php script that connects the required fields from FB.

<?php
$host = 'MYPATH';
    $username = 'username here';
    $password = 'password here';
    $dbh = ibase_connect($host, $username, $password);
    $stmt = 'SELECT CLCTABRE FROM CLIENT';
    $sth = ibase_query($dbh, $stmt);
    while ($row = ibase_fetch_object($sth)) {
       echo $row->CLCTABRE . "\n";
    }
    ibase_free_result($sth);
    ibase_close($dbh);
?>

Open in new window


How would I initiate a connection to my mysql database and insert the selected fields?

I also need it to check if the customers exists in mysql first, if it does then do not insert any fields.

I understand this a broad question , and I'm not expecting an specific answers If anyone has any examples then that would be great.

Thank you
0
Comment
Question by:dan_stan
  • 5
  • 4
10 Comments
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39621516
OK. A simple way is to add your values to an array, and then loop through that array to add them to the new mySQL database. In your code, instead of echoing out the value, change it to this:

while ($row = ibase_fetch_object($sth)) {
   $newValues[] = $row->CLCTABRE;
}

Open in new window

You can then use this code to add them to the new mySQL table
$dbh = new PDO('mysql:host=localhost;dbname=yourDB', 'user', 'pass');

$stmt = $dbh->prepare("INSERT INTO yourTable (yourColumn) VALUES (:yourValue)");
$stmt->bindParam('yourValue', $value);

try {
	foreach ($newvalues as $value):
		$stmt->execute();
	endforeach;
} catch (PDOException $e) {
	if ($e->errorInfo[1] != 1062) {
		//silently skip duplicate entries
		echo $e->getMessage();
	}
}

Open in new window

To prevent duplicates, setup your database column to be unique. A 1062 error will then be generated when you try to add a duplicate and the code above effectively ignores this error, so they won't be added.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39621545
0
 

Author Comment

by:dan_stan
ID: 39621707
Thanks guys, I will take a look at both suggestions and get back to you.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dan_stan
ID: 39621754
@ChrisStanyon

This is my statement

<?php
$host = '';
    $username = '';
    $password = '';
    $dbh = ibase_connect($host, $username, $password);
    $stmt = 'SELECT FIRST 10 CLCTABRE ,CLCTNOM  FROM CLIENT';
    $sth = ibase_query($dbh, $stmt);
    while ($row = ibase_fetch_object($sth)) {
   $newValues[] = $row->CLCTABRE;
}
    ibase_free_result($sth);
    ibase_close($dbh);
	
$dbh = new PDO('mysql:host=localhost;dbname=swdata', 'root', 'root');

$stmt = $dbh->prepare("INSERT INTO USERDB (keysearch) VALUES (:yourValue)");
$stmt->bindParam('yourValue', $value);

try {
	foreach ($newvalues as $value):
		$stmt->execute();
	endforeach;
} catch (PDOException $e) {
	if ($e->errorInfo[1] != 1062) {
		//silently skip duplicate entries
		echo $e->getMessage();
	}
}
?>

Open in new window


I'm getting the following error -

Notice: Undefined variable: newvalues in C:\xampp\htdocs\precious\customer.php on line 20

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\precious\customer.php on line 20

can you help me?
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39621774
No. The prepare() statement has just prepared the query and told it to expect a named parameter called :yourValue. You then bind this named parameter to a variable:

$stmt->bindParam('yourValue', $value);

When the query is executed it will replace :yourValue in the query with whatever value is stored in the $value variable. You then loop over your $newValues array, like this:

foreach ($newvalues as $value):
      $stmt->execute();
endforeach;

Each time it loops over your array, it takes the value from it and stores it in the $value variable, so each time the query is executed, whatever is stored in $value is pushed into the query, in place of the named parameter.

Hope that makes sense.
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39621778
Your error is because variable names are case sensitive. You are setting it like this:

$newValues[] = $row->CLCTABRE;

and trying to use it like this:

foreach ($newvalues as $value):

One has a capital V, the other doesn't!
0
 

Author Comment

by:dan_stan
ID: 39621837
works like a dream. thanks a lot.

I really did not think I could get this working so quickly!
0
 

Author Comment

by:dan_stan
ID: 39622048
Just a quick question, if I wanted to insert more than one column, how would I adapt the script?
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39622285
Firstly, you'd need to edit your SQL string, adding in the columns you want to set and the named parameters for those columns. You'd then need to bind the parameters to variables:

$stmt = $dbh->prepare("INSERT INTO USERDB (column1, column2, column3) VALUES (:value1, :value2, :value3)");
$stmt->bindParam('value1', $x_variable);
$stmt->bindParam('value2', $y_variable);
$stmt->bindParam('value3', $z_variable);

Open in new window

Then you'd need to assign values to these variables before you execute the query. Can't see from your script where you're getting the data from, but assuming you built a multi-dimensional array, then you would do something like:

foreach ($newvalues as $data):
	$x_variable = $data['key1'];
	$y_variable = $data['key2'];
	$z_variable = $data['key3'];
	$stmt->execute();
endforeach;

Open in new window

0
 

Author Comment

by:dan_stan
ID: 39623680
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question