Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

insert data from Firebird to mysql

Posted on 2013-11-04
10
Medium Priority
?
1,019 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

722 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