Solved

interbase / mysql php script

Posted on 2013-11-06
6
615 Views
Last Modified: 2013-11-06
Hi,

I originally posted a question in relation to selecting data from an interbase database and then inserting it in to a mysql database.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28284651.html

So far I am able to insert a single selected value by using the following script -

<?php
include_once 'database_connect.php';
      $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


However I want to be able to insert more fields in to my insert script, but am getting an error message.
Warning: Illegal string offset 'value1'
Warning: Illegal string offset 'value2'
Warning: Illegal string offset 'value3'

Can someone please give me a hand resolving this?

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

$stmt = $dbh->prepare("INSERT INTO USERDB (keysearch ,firstname ,surname) VALUES (:value1, :value2, :value3)");
$stmt->bindParam('value1', $x_variable);
$stmt->bindParam('value2', $y_variable);
$stmt->bindParam('value3', $z_variable);
try {
	foreach ($newvalues as $data):
	$x_variable = $data['value1'];
	$y_variable = $data['value2'];
	$z_variable = $data['value3'];
	$stmt->execute();
endforeach;
} catch (PDOException $e) {
	if ($e->errorInfo[1] != 1062) {
		//silently skip duplicate entries
		echo $e->getMessage();
	}
}
?>

Open in new window

0
Comment
Question by:dan_stan
  • 3
  • 3
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
What are the lines of code that generate the error messages? The line numbers appear in the error messages, and we would need to see the line of code and the complete message to understand the error.
0
 

Author Comment

by:dan_stan
Comment Utility
sorry my mistake!

Warning: Illegal string offset 'value1' in C:\xampp\htdocs\precious\customer.php on line 22

Warning: Illegal string offset 'value2' in C:\xampp\htdocs\precious\customer.php on line 23

Warning: Illegal string offset 'value3' in C:\xampp\htdocs\precious\customer.php on line 24
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Got it, thanks.  Try it a little more like this.  Obviously this is untested code because I do not have your database, but I think it's correct in principle.  Not sure about the exception handler - that would be worth specific testing.

<?php // RAY_temp_dan_stan.php
error_reporting(E_ALL);
include_once 'database_connect.php';

// COLLECT SOME DATA FROM IBASE
$dbh  = ibase_connect($host, $username, $password);
$stmt = 'SELECT FIRST 10 CLCTABRE ,CLCTGROUPE ,CLCTVILLIV FROM CLIENT';
$sth  = ibase_query($dbh, $stmt);

// CREATE AN ARRAY OF OBJECTS
while ($row = ibase_fetch_object($sth))
{
    $newvalues[] = $row;
}

// THIS MAY NOT BE NEEDED - PHP GARBAGE COLLECTOR WILL HANDLE IT
ibase_free_result($sth);
ibase_close($dbh);

// START MYSQL
$dbh = new PDO('mysql:host=localhost;dbname=swdata', 'root', 'root');

// PREPARE A QUERY
$qry = "INSERT INTO USERDB (keysearch ,firstname ,surname) VALUES (:CLCTABRE, :CLCTGROUPE, :CLCTVILLIV)";
$stmt = $dbh->prepare($qry);

// USE THE ARRAY OF OBJECTS
foreach ($newvalues as $obj)
{
    // BIND THE OBJECT PROPERTIES INTO THE QUERY
    $stmt->bindParam('CLCTABRE',   $obj->CLCTABRE);
    $stmt->bindParam('CLCTGROUPE', $obj->CLCTGROUPE);
    $stmt->bindParam('CLCTVILLIV', $obj->CLCTVILLIV);

    // TRY THE QUERY
    try
    {
        $stmt->execute();
    }

    // IF THERE IS AN EXCEPTION THROWN BY PDO
    catch (PDOException $exc)
    {
        // IF THE EXCEPTION NUMBER IS NOT "DUPLICATE UNIQUE"
        if ($exc->errorInfo[1] != 1062)
        {
            $msg = "FAIL: $qry " . PHP_EOL . $exc->getmessage();
            trigger_error($msg, E_USER_ERROR);
        }
    }
}

Open in new window

HTH, ~Ray
0
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

 

Author Comment

by:dan_stan
Comment Utility
Ray, that worked perfectly, thanks for your help!
0
 

Author Comment

by:dan_stan
Comment Utility
PS I'm about to post another question regarding this scrip!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Great! Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Read about achieving the basic levels of HRIS security in the workplace.
This article discusses four methods for overlaying images in a container on a web page
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…
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now