Click to enter my new blog: http://www.sofee.cn/blog/
Recently, when developing your own sofeeframework, You need to retrieve the XML format from the database to use simplexml for processing. Since this format has not been added to PDO so far, simply install a patch for the PDO. The patch is made on a PDO-1.0RC1 basis.
Example:
========================================================== ====================
<? PHP
$ X = new PDO ("SQLite: Memory :");
$ X-> query ("create table test (name string, value string )");
$ Stmt = $ X-> prepare ("insert into test (name, value) values (: name,: Value )");
$ Stmt-> bindparam (": Name", $ the_name, pdo_param_str, 32 );
$ Stmt-> bindparam (": value", $ the_value, pdo_param_str, 32 );
For ($ I = 0; $ I <4; $ I ++ ){
$ The_name = "foo". Rand ();
$ The_value = "bar". Rand ();
If (! $ Stmt-> execute ()){
Break;
}
}
$ Stmt = $ X-> query ("select * from test ");
Header ("Content-Type: text/XML ;");
Echo $ stmt-> fetchall (pdo_fetch_xml );
?>
Output:
========================================================== ======================================
-<Resultset querystring = "select * from test">
-<Row>
<Name> foo30156 </Name>
<Value> bar21 </value>
</Row>
-<Row>
<Name> foo7978 </Name>
<Value> bar16667 </value>
</Row>
-<Row>
<Name> foo8888 </Name>
<Value> bar2193 </value>
</Row>
-<Row>
<Name> foo1014 </Name>
<Value> bar21495 </value>
</Row>
</Resultset>
The content of the patch file is as follows:
========================================================== ========================================================
Diff-Nur PDO-1.0RC1/PDO. c PdO/PDO. c
--- PDO-1.0RC1/PDO. c sun sep 11 13:27:30 2005
+++ PdO/PDO. C mon Oct 24 19:18:38 2005
@-320,6 + 320,7 @@
Register_long_constant ("pdo_fetch_serialize", (long) pdo_fetch_serialize, const_cs | const_persistent );
# Endif
Register_long_constant ("pdo_fetch_named", (long) pdo_fetch_named, const_cs | const_persistent );
+ Register_long_constant ("pdo_fetch_xml", (long) pdo_fetch_xml, const_cs | const_persistent );
Register_long_constant ("pdo_attr_autocommit", (long) pdo_attr_autocommit, const_cs | const_persistent );
Register_long_constant ("pdo_attr_prefetch", (long) pdo_attr_prefetch, const_cs | const_persistent );
Diff-Nur PDO-1.0RC1/pdo_stmt.c PdO/pdo_stmt.c
--- PDO-1.0RC1/pdo_stmt.c sun sep 11 03:32:16 2005
++ PdO/pdo_stmt.c Tue Oct 25 11:13:49 2005
-36,6 + 36,7 @@
# Include "zend_exceptions.h"
# Include "zend_interfaces.h"
# Include "php_memory_streams.h"
+ # Include "ext/standard/php_smart_str.h"
# If compile_dl_pdo
/* {Content from zend_arg_defs.c:
@-743,6 + 744,7 @@
Int flags = How & pdo_fetch_flags, idx, old_arg_count;
Zend_class_entry * ce, * old_ce;
Zval grp_val, * GRP, ** pgrp, * retval, * old_ctor_args;
+ Smart_str xmlstr = {0 };
How = How &~ Pdo_fetch_flags;
If (how = pdo_fetch_use_default ){
@-846,6 + 848,11 @@
}
}
Break;
+
+ Case pdo_fetch_xml:
+ Convert_to_string (return_value );
+ Smart_str_appends (& xmlstr, "<row> ");
+ Break;
Default:
@-969,6 + 976,16 @@
Stmt-> fetch. func. Values [idx] = val;
Stmt-> fetch. Cls. FCI. Params [idx] = & stmt-> fetch. func. Values [idx];
Break;
+
+ Case pdo_fetch_xml:
+ Smart_str_appends (& xmlstr, "<");
+ Smart_str_appendl (& xmlstr, stmt-> Columns [I]. Name, stmt-> Columns [I]. namelen );
+ Smart_str_appends (& xmlstr, "> ");
+ Smart_str_appendl (& xmlstr, z_strval_p (VAL), z_strlen_p (VAL ));
+ Smart_str_appends (& xmlstr, "</");
+ Smart_str_appendl (& xmlstr, stmt-> Columns [I]. Name, stmt-> Columns [I]. namelen );
+ Smart_str_appends (& xmlstr, "> ");
+ Break;
Default:
Zval_ptr_dtor (& Val );
@-, 6 + @@
Zval_ptr_dtor (& stmt-> fetch. func. Values [idx]);
}
Break;
+
+ Case pdo_fetch_xml:
+ Smart_str_appends (& xmlstr, "</row> ");
+ Smart_str_0 (& xmlstr );
+ Zval_stringl (return_value, xmlstr. C, xmlstr. Len, 1 );
+ Smart_str_free (& xmlstr );
+ Break;
Default:
Break;
@-1231,6 + 1255,7 @@
Zend_class_entry * old_ce;
Zval * old_ctor_args, * ctor_args = NULL;
Int error = 0, old_arg_count;
+ Smart_str xmlstr = {0 };
If (failure = zend_parse_parameters (zend_num_args () tsrmls_cc, "| lzz", & how, & arg2, & ctor_args )){
Return_false;
@-1318,7 + 1343,6 @@
Error = 1;
}
}
-
If (! Error ){
Pdo_stmt_clear_err ();
Make_std_zval (data );
@-1339,11 + 1363,29 @@
Make_std_zval (data );
} While (do_fetch (stmt, true, Data, how, pdo_fetch_ori_next, 0, return_all tsrmls_cc ));
} Else {
-Array_init (return_value );
+ If (how = pdo_fetch_xml ){
+ Convert_to_string (return_value );
+ Smart_str_appends (& xmlstr, "<resultset querystring = /"");
+ Smart_str_appendl (& xmlstr, stmt-> QUERY_STRING, stmt-> query_stringlen );
+ Smart_str_appends (& xmlstr, "/"> ");
+} Else {
+ Array_init (return_value );
+}
Do {
-Add_next_index_zval (return_value, data );
+ If (how = pdo_fetch_xml ){
+ Smart_str_appends (& xmlstr, z_strval_p (data ));
+} Else {
+ Add_next_index_zval (return_value, data );
+}
Make_std_zval (data );
} While (do_fetch (stmt, true, Data, how, pdo_fetch_ori_next, 0, 0 tsrmls_cc ));
+
+ If (how = pdo_fetch_xml ){
+ Smart_str_appends (& xmlstr, "</resultset> ");
+ Smart_str_0 (& xmlstr );
+ Zval_stringl (return_value, xmlstr. C, xmlstr. Len, 1 );
+ Smart_str_free (& xmlstr );
+}
}
Free_zval (data );
}
Diff-Nur PDO-1.0RC1/php_pdo_driver.h PdO/php_pdo_driver.h
--- PDO-1.0RC1/php_pdo_driver.h Wed Jul 20 11:38:34 2005
++ PdO/php_pdo_driver.h mon Oct 24 19:18:29 2005
@-90,6 + 90,7 @@
Pdo_fetch_into,/* fetch row into an existing object */
Pdo_fetch_func,/* fetch into function and return its result */
Pdo_fetch_named,/* Like pdo_fetch_assoc, but can handle duplicate names */
+ Pdo_fetch_xml,/* fetch row into XML format */
Pdo_fetch1_max/* must be last */
};