Solved

insert data from Firebird to mysql

Posted on 2013-11-04
10
970 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 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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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 …

813 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

13 Experts available now in Live!

Get 1:1 Help Now