PDO示例
====================

您可前往电科金仓官网下载 `PHP测试用例 <https://kingbase.oss-cn-beijing.aliyuncs.com/KES_INTERFACE/testyongli/php/php.zip>`_ 。

下面给出示例，描述了如何通过 KingbaseES PDO 访问 KingbaseES 数据库。

a. 数据库连接示例

   .. code:: PROGRAMLISTING

        $dsn = 'kdb:host=localhost;dbname=TEST;port=54321';
        $user = 'SYSTEM';
        $password = '123456';
        $dbh = null;
        try {
            $dbh = new PDO($dsn,$user, $password, array(PDO::ATTR_ERRMODE => 
                PDO::ERRMODE_EXCEPTION));
        } catch (Exception $e) {
            echo 'Connection Failed: ' . $e->getMessage() ."\n";
        }
        return $dbh;

b. 大对象操作示例

   .. code:: PROGRAMLISTING

        $stmt = $dbh->prepare("insert into TESTBLOB (id, bb, cb) " . "VALUES 
        (?, ?, ?)");
        $id=1;
        $fp = fopen("./testblob.png", "rb") or die("Unable to open file!");
        $fp2 = "clob test,aaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbb
        cccccccccccccccccdddddddddddddddd";
        $stmt->bindParam(1, $id);
        $stmt->bindParam(2, $fp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $fp2, PDO::PARAM_STR,strlen($fp2));

        $dbh->beginTransaction();
        $stmt->execute();
        $dbh->commit();
        fclose($fp);


        $stmt = $dbh->prepare("select bb, cb from TESTBLOB where id=1");
        $stmt->execute();
        $stmt->bindColumn(1, $bb, PDO::PARAM_LOB);
        $stmt->bindColumn(2, $cb, PDO::PARAM_LOB);
        $stmt->fetch(PDO::FETCH_BOUND);

        /* blob */
        $wblobfp = fopen("blob_out.jpg", "wb+");
        stream_copy_to_stream($bb, $wblobfp);

        /* clob */
        $wclobfp = fopen("./testclob_out.log", "w+");
        fwrite($wclobfp, $cb);
