Driver 4D for PDO
PHP Manual

Examples with PDO_4D

()

Examples with PDO_4DExamples PDO_4D

This basic example show how to connect, execute a query, read data and disconnect from a 4D SQL server.

例1 Basic example with PDO_4D

<?php
$dsn 
'4D:host=localhost;charset=UTF-8';
$user 'test';
$pass 'test';

// Connection to the 4D SQL server
$db = new PDO_4D($dsn$user$pass);

try {
    
$db->exec('CREATE TABLE test(id varCHAR(1) NOT NULL, val VARCHAR(10))');
} catch (
PDOException $e) {
    die(
"Erreur 4D : " $e->getMessage());
}
        
$db->exec("INSERT INTO test VALUES('A', 'A')");
$db->exec("INSERT INTO test VALUES('B', 'A')");
$db->exec("INSERT INTO test VALUES('C', 'C')");

$stmt $db->prepare('SELECT id, val from test');

$stmt->execute();
print_r($stmt->fetchAll());

unset(
$stmt);
unset(
$db);
?>

上の例の出力は以下となります。

    Array
(
    [0] => Array
        (
            [ID] => A
            [0] => A
            [VAL] => B
            [1] => B
        )

    [1] => Array
        (
            [ID] => C
            [0] => C
            [VAL] => D
            [1] => D
        )

    [2] => Array
        (
            [ID] => E
            [0] => E
            [VAL] => F
            [1] => F
        )

)

This example shows how to execute a query in 4D language, and how to read the result through PDO_4D.

例2 Accessing 4D language from pdo_4d

Set up a 4D method, called method. Make sure in the method properties that the option Available via SQL is checked. The 4D code is the following.

C_TEXTE($0)
$0:=Version application(*);

The PHP code to use the above 4D method is :

<?php
$dsn 
'4D:host=localhost;charset=UTF-8';
$user 'test';
$pass 'test';

// Connection to the 4D server
$db = new pdo($dsn$user$pass);

$stmt $db->prepare('SELECT {FN method() AS VARCHAR } FROM _USER_SCHEMAS LIMIT 1');

$stmt->execute();
print_r($stmt->fetchAll())    ;

unset(
$stmt);
unset(
$db);
?>

上の例の出力は以下となります。

(
    [0] => Array
        (
            [<expression>] => F0011140
            [0] => F0011140
        )

)

例3 Executing a LEFT JOIN in 4D

This example shows how to execute a LEFT JOIN SQL query, with 4D.

<?php
$dsn 
'4D:host=localhost;charset=UTF-8';
$user 'test';
$pass 'test';
 
// Connection to the 4D SQL server
$db = new PDO_4D($dsn$user$pass);
 
/* The SQL query is the equivalent of the following LEFT JOIN
SELECT
  classtypes.name,
  test.id AS id,
  test.val AS val
FROM
  test
LEFT JOIN
  classtypes
ON
  test.classtype=classtypes.id
*/
 
// Using SQL-89 Notation
$query = <<<SQL
SELECT
  classtypes.name,
  test.id AS id,
  test.val AS val
FROM
  test,
  classtypes
WHERE
  test.classtype=classtypes.id
  OR test.classtype IS NULL
SQL;
 
$stmt 
$db->prepare($query);
$stmt->execute();
       
print_r
($stmt->fetchAll());
 
$stmt 
null;
$db null;
?>

例4 Escaping 4D table names

This examples illustrates how to escape characters in a 4D SQL query.

<?php
$dsn 
'4D:host=localhost;charset=UTF-8';
$user 'test';
$pass 'test';
 
// Connection to 4D server 4D
$db = new PDO_4D($dsn$user$pass);

$objects = array('[',']','[]','][','[[',']]','[[[',']]]','TBL ]]32[23');

foreach(
$objects as $id => $object) {
    
$object str_replace(']',']]'$object);
    print 
"$object\n";
    
    
$db->exec('CREATE TABLE IF NOT EXISTS ['.$object.'](['.$object.'] FLOAT)');

    
$req "INSERT INTO [$object] ([$object]) VALUES ($id);";
    
$db->query($req);

       
$q $db->prepare("SELECT [$object] FROM [$object]");
    
$q->execute();
    
$x[] = $q->fetch(PDO::FETCH_NUM);

    
$db->exec('DROP TABLE ['.$object.'];');
}

?>

上の例の出力は以下となります。

[
]]
[]]
]][
[[
]]]]
[[[
]]]]]]
TBL ]]]]32[23


Driver 4D for PDO
PHP Manual