mirror of
https://github.com/php/doc-de.git
synced 2026-03-24 07:12:15 +01:00
138 lines
4.5 KiB
XML
138 lines
4.5 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: eae19eb5fe0f5bebcbce382ea7a505cedeb5a883 Maintainer: fa Status: ready -->
|
|
|
|
<chapter xml:id="pdo.lobs" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<title>Large Objects (LOBs)</title>
|
|
<para>
|
|
Es könnte an irgendeinem Punkt in Ihrer Anwendung passieren, dass Sie eine
|
|
"große" Menge an Daten in Ihrer Datenbank ablegen müssen. Groß bedeutet
|
|
typischerweise "etwa 4 kb oder mehr", obwohl manche Datenbanken spielend bis
|
|
zu 32 kb bearbeiten können, bevor das als "groß" zählt. Large Objects können
|
|
entweder textueller oder binärer Natur sein. PDO erlaubt Ihnen, mit diesem
|
|
großen Datentyp zu arbeiten, indem Sie <constant>PDO::PARAM_LOB</constant>
|
|
als Typ in Ihren Methodenaufrufen von
|
|
<methodname>PDOStatement::bindParam</methodname> oder
|
|
<methodname>PDOStatement::bindColumn</methodname> benutzen.
|
|
<constant>PDO::PARAM_LOB</constant> veranlasst PDO, die Daten als Stream zu
|
|
behandeln, so dass Sie diese mit Hilfe der <link linkend="ref.stream">PHP
|
|
Streams-API</link> bearbeiten können.
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Ein Bild aus einer Datenbank anzeigen</title>
|
|
<para>
|
|
Dieses Beispiel weist das LOB der Variable namens $lob zu und sendet es
|
|
mittels <function>fpassthru</function> an den Browser. Weil das LOB als
|
|
Stream dargestellt wird, können Funktionen wie <function>fgets</function>,
|
|
<function>fread</function> und <function>stream_get_contents</function>
|
|
damit benutzt werden.
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
|
|
$stmt = $db->prepare("select contenttype, imagedata from images where id=?");
|
|
$stmt->execute(array($_GET['id']));
|
|
$stmt->bindColumn(1, $type, PDO::PARAM_STR, 256);
|
|
$stmt->bindColumn(2, $lob, PDO::PARAM_LOB);
|
|
$stmt->fetch(PDO::FETCH_BOUND);
|
|
|
|
header("Content-Type: $type");
|
|
fpassthru($lob);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Ein Bild in eine Datenbank einfügen</title>
|
|
<para>
|
|
Dieses Beispiel öffnet eine Datei und übergibt das File-Handle an PDO,
|
|
damit sie als LOB eingefügt wird. PDO wird sein Möglichstes tun, den
|
|
Inhalt der Datei auf möglichst effiziente Weise in die Datenbank zu
|
|
bekommen.
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
|
|
$stmt = $db->prepare("insert into images (id, contenttype, imagedata) values (?, ?, ?)");
|
|
$id = get_new_id(); // Eine Funktion zum Allokieren der neuen ID
|
|
|
|
// Wir nehmen an, das Skript läuft als Teil eines Datei-Upload-Formulars
|
|
// Sie finden weitere Informationen in der PHP-Dokumentation
|
|
|
|
$fp = fopen($_FILES['file']['tmp_name'], 'rb');
|
|
|
|
$stmt->bindParam(1, $id);
|
|
$stmt->bindParam(2, $_FILES['file']['type']);
|
|
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);
|
|
|
|
$db->beginTransaction();
|
|
$stmt->execute();
|
|
$db->commit();
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Ein Bild in eine Datenbank einfügen: Oracle</title>
|
|
<para>
|
|
Oracle erfordert eine leicht unterschiedliche Syntax, um ein LOB aus einer
|
|
Datei einzufügen. Es ist auch unumgänglich, dass Sie diesen Insert
|
|
innerhalb einer Transaktion durchführen, andernfalls wird Ihr frisch
|
|
eingefügtes LOB mit einer Länge von 0 als Teil des impliziten Commits,
|
|
der beim Ausführen der Abfrage passiert, gespeichert.
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$db = new PDO('oci:', 'scott', 'tiger');
|
|
$stmt = $db->prepare("insert into images (id, contenttype, imagedata) " .
|
|
"VALUES (?, ?, EMPTY_BLOB()) RETURNING imagedata INTO ?");
|
|
$id = get_new_id(); // Eine Funktion zum Allokieren einer neuen ID
|
|
|
|
// Wir nehmen an, das Skript läuft als Teil eines Datei-Upload-Formulars
|
|
// Sie finden weitere Informationen in der PHP-Dokumentation
|
|
|
|
$fp = fopen($_FILES['file']['tmp_name'], 'rb');
|
|
|
|
$stmt->bindParam(1, $id);
|
|
$stmt->bindParam(2, $_FILES['file']['type']);
|
|
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);
|
|
|
|
$db->beginTransaction();
|
|
$stmt->execute();
|
|
$db->commit();
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</chapter>
|
|
|
|
<!-- 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
|
|
-->
|