Solved

insert data from Firebird to mysql

Posted on 2013-11-04
10
997 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 43

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 110

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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 43

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 43

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 43

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

729 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