Solved

interbase / mysql php script

Posted on 2013-11-06
6
630 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
[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
  • 3
  • 3
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39626721
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
ID: 39626723
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 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39626764
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:dan_stan
ID: 39626845
Ray, that worked perfectly, thanks for your help!
0
 

Author Comment

by:dan_stan
ID: 39626931
PS I'm about to post another question regarding this scrip!
0
 
LVL 110

Expert Comment

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

Featured Post

Webinar: Choosing a MySQL HA Solution

Join Percona’s Principal Technical Services Engineer, Marcos Albe as he presents Choosing a MySQL High Availability Solution on Thursday, June 29, 2017 at 10:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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…
The viewer will learn how to dynamically set the form action using jQuery.

688 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