1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 296385 $ -->
<refentry xml:id="pdo-4d.examples" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>Examples with PDO_4D</refname>
<refpurpose>Examples PDO_4D</refpurpose>
</refnamediv>
<refsect1 role="description">
<para>
This basic example show how to connect, execute a query,
read data and disconnect from a 4D SQL server.
<example>
<title>Basic example with PDO_4D</title>
<programlisting role="php">
<![CDATA[
<?php
$dsn = '4D:host=localhost;charset=UTF-8';
$user = 'test';
$pass = 'test';
// Connection to the 4D SQL server
$db = new PDO($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);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
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
)
)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="description">
<para>
This example shows how to execute a query in 4D language,
and how to read the result through PDO_4D.
<example>
<title>Accessing 4D language from pdo_4d</title>
<para>
Set up a 4D method, called <literal>method</literal>. Make sure
in the method properties that the option
<literal>Available via SQL</literal> is checked. The 4D code
is the following.
</para>
<programlisting role="4d">
<![CDATA[
C_TEXTE($0)
$0:=Version application(*);
]]>
</programlisting>
<para>
The PHP code to use the above 4D method is :
</para>
<programlisting role="php">
<![CDATA[
<?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);
?>]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
(
[0] => Array
(
[<expression>] => F0011140
[0] => F0011140
)
)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="description">
<para>
<example>
<title>Escaping 4D table names</title>
<para>
This examples illustrates how to escape characters in a
4D SQL query.
</para>
<programlisting role="php">
<![CDATA[
<?php
$dsn = '4D:host=localhost;charset=UTF-8';
$user = 'test';
$pass = 'test';
// Connection to 4D server 4D
$db = new PDO($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.'];');
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
[
]]
[]]
]][
[[
]]]]
[[[
]]]]]]
TBL ]]]]32[23
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|