?
Solved

interbase / mysql php script

Posted on 2013-11-06
6
Medium Priority
?
636 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 111

Expert Comment

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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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