Solved

insert data from Firebird to mysql

Posted on 2013-11-04
10
957 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 42

Accepted Solution

by:
Chris Stanyon earned 500 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 108

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
 

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 42

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 42

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 42

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Read about achieving the basic levels of HRIS security in the workplace.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now