Solved

interbase / mysql php script

Posted on 2013-11-06
6
622 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 109

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 109

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
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: 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 109

Expert Comment

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

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ebay devID, appID, certID, userToken 2 25
PHP Form Calculate Total Price 10 40
Querying data from 3 SQL tables 2 31
Decrypt string by php 7 29
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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.

786 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