<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >SDO_DAS_Relational::executePreparedQuery</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="SDO Relational Data Access Service Functions" HREF="ref.sdo.das.rel.html"><LINK REL="PREVIOUS" TITLE="SDO_DAS_Relational::createRootDataObject" HREF="function.sdo-das-relational-createrootdataobject.html"><LINK REL="NEXT" TITLE="SDO_DAS_Relational::executeQuery" HREF="function.sdo-das-relational-executequery.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.sdo-das-relational-createrootdataobject.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.sdo-das-relational-executequery.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.SDO-DAS-Relational-executePreparedQuery" ></A >SDO_DAS_Relational::executePreparedQuery</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN200578" ></A ><P > (no version information, might be only in CVS)</P >SDO_DAS_Relational::executePreparedQuery -- Executes an SQL query passed as a prepared statement, with a list of values to substitute for placeholders, and return the results as a normalised data graph. </DIV ><DIV CLASS="refsect1" ><A NAME="AEN200581" ></A ><H2 >Descrição</H2 >SDODataObject <B CLASS="methodname" >SDO_DAS_Relational::executePreparedQuery</B > ( PDO database_handle, PDOStatement prepared_statement, array value_list [, array column_specifier] )<BR ></BR ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Atenção</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Esta função é <SPAN CLASS="emphasis" ><I CLASS="emphasis" >EXPERIMENTAL</I ></SPAN >. Isso quer dizer que o comportamento desta função e seu nome, incluindo TUDO o que está documentado aqui pode mudar em futuras versões do PHP, SEM QUALQUER NOTIFICAÇÃO. Esteja avisado, e use esta função por sua própria conta e risco.</P ></TD ></TR ></TABLE ></DIV ><P > Executes a given query against the relational database, using the supplied PDO database handle. Differs from the simpler <B CLASS="function" >executeQuery()</B > in that it takes a prepared statement and a list of values. This is the appropriate call to use either when the statement is to executed a number of times with different arguments, and there is therefore a performance benefit to be had from preparing the statement only once, or when the the SQL statement is to contain varying values taken from a source that cannot be completely trusted. In this latter case it may be unsafe to construct the SQL statement by simply concatenating the parts of the statement together, since the values may contain pieces of SQL. To guard against this, a so-called SQL injection attack, it is safer to prepare the SQL statement with placeholders (also known as parameter markers, denoted by '?') and supply a list of the values to be substituted as a separate argument. Otherwise this function is the same as <B CLASS="function" >executeQuery()</B > in that it uses the model that it built from the the metadata to interpret the result set and returns a data graph. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN200604" ></A ><H2 >Parâmetros</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT >PDO_database_handle</DT ><DD ><P > Constructed using the PDO extension. A typical line to construct a PDO database handle might look like this: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> $dbh = new PDO("mysql:dbname=COMPANYDB;host=localhost",DATABASE_USER,DATABASE_PASSWORD);</font> </code></TD ></TR ></TABLE > </P ></DD ><DT >prepared_statement</DT ><DD ><P > A prepared SQL statement to be executed against the database. This will have been prepared by PDO's <B CLASS="function" >prepare()</B > method. </P ></DD ><DT >value_list</DT ><DD ><P > An array of the values to be substituted into the SQL statement in place of the placeholders. In the event that there are no placeholders or parameter markers in the SQL statement then this argument can be specified as <TT CLASS="constant" ><B >NULL</B ></TT > or as an empty array; </P ></DD ><DT >column_specifier</DT ><DD ><P > The Relational DAS needs to examine the result set and for every column, know which table and which column of that table it came from. In some circumstances it can find this information for itself, but sometimes it cannot. In these cases a column specifier is needed, which is an array that identifies the columns. Each entry in the array is simply a string in the form <CODE CLASS="varname" >table-name.column_name</CODE >. </P ><P > The column specifier is needed when there are duplicate column names in the database metadata, For example, in the database used within the examples, all the tables have both a <CODE CLASS="varname" >id</CODE > and a <CODE CLASS="varname" >name</CODE > column. When the Relational DAS fetches the result set from PDO it can do so with the PDO_FETCH_ASSOC attribute, which will cause the columns in the results set to be labelled with the column name, but will not distinguish duplicates. So this will only work when there are no duplicates possible in the results set. </P ><P > To summarise, specify a column specifier array whenever there is any uncertainty about which column could be from which table and only omit it when every column name in the database metadata is unique. </P ><P > All of the examples in the <A HREF="ref.sdo.das.rel.html#sdo.das.rel.examples" >Examples</A > use a column specifier. There is one example in the <TT CLASS="filename" >Scenarios</TT > directory of the installation that does not: that which works with just the employee table, and because it works with just one table, there can not exist duplicate column names. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN200635" ></A ><H2 >Valores de retornado</H2 ><P > Returns a data graph. Specifically, it returns a root object of a special type. Under this root object will be the data from the result set. The root object will have a multi-valued containment property with the same name as the application root type specified on the constructor, and that property will contain one or more data objects of the application root type. </P ><P > In the event that the query returns no data, the special root object will still be returned but the containment property for the application root type will be empty. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN200639" ></A ><H2 >Erros/Excessões</H2 ><P > <B CLASS="function" >SDO_DAS_Relational::executePreparedQuery()</B > can throw an SDO_DAS_Relational_Exception if it is unable to construct the data graph correctly. This can occur for a number of reasons: for example if it finds that it does not have primary keys in the result set for all the objects. It also catches any PDO exceptions and obtains PDO diagnostic information which it includes in an SDO_DAS_Relational_Exception which it then throws. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN200643" ></A ><H2 >Exemplos</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN200646" ></A ><P ><B >Exemplo 1. Retrieving a data object using <B CLASS="function" >executePreparedQuery()</B ></B ></P ><P > In this example a single data object is retrieved from the database - or possibly more than one if there is more than one company called 'Acme'. For each company returned, the <CODE CLASS="varname" >name</CODE > and <CODE CLASS="varname" >id</CODE > properties are echoed. </P ><P > Other examples of the use of <B CLASS="function" >executePreparedQuery()</B > can be found in the example code supplied in <TT CLASS="filename" >sdo/DAS/Relational/Scenarios</TT > . </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">require_once </font><font color="#DD0000">'SDO/DAS/Relational.php'</font><font color="#007700">;<br />require_once </font><font color="#DD0000">'company_metadata.inc.php'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/**************************************************************<br /> * Construct the DAS with the metadata<br /> ***************************************************************/<br /></font><font color="#0000BB">$das </font><font color="#007700">= new </font><font color="#0000BB">SDO_DAS_Relational </font><font color="#007700">(</font><font color="#0000BB">$database_metadata</font><font color="#007700">,</font><font color="#DD0000">'company'</font><font color="#007700">,</font><font color="#0000BB">$SDO_reference_metadata</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/**************************************************************<br /> * Get a database connection<br /> ***************************************************************/<br /></font><font color="#0000BB">$dbh </font><font color="#007700">= new </font><font color="#0000BB">PDO</font><font color="#007700">(</font><font color="#0000BB">PDO_DSN</font><font color="#007700">,</font><font color="#0000BB">DATABASE_USER</font><font color="#007700">,</font><font color="#0000BB">DATABASE_PASSWORD</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/**************************************************************<br /> * Issue a query to obtain a company object - possibly more if they exist<br /> * Use a prepared query with a placeholder.<br /> ***************************************************************/<br /></font><font color="#0000BB">$name </font><font color="#007700">= </font><font color="#DD0000">'Acme'</font><font color="#007700">;<br /></font><font color="#0000BB">$pdo_stmt </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-></font><font color="#0000BB">prepare</font><font color="#007700">(</font><font color="#DD0000">'select name, id from company where name=?'</font><font color="#007700">);<br /></font><font color="#0000BB">$root </font><font color="#007700">= </font><font color="#0000BB">$das</font><font color="#007700">-></font><font color="#0000BB">executePreparedQuery</font><font color="#007700">(<br /> </font><font color="#0000BB">$dbh</font><font color="#007700">, <br /> </font><font color="#0000BB">$pdo_stmt</font><font color="#007700">,<br /> array(</font><font color="#0000BB">$name</font><font color="#007700">), <br /> array(</font><font color="#DD0000">'company.name'</font><font color="#007700">, </font><font color="#DD0000">'company.id'</font><font color="#007700">));<br /><br /></font><font color="#FF8000">/**************************************************************<br /> * Echo name and id <br /> ***************************************************************/<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$root</font><font color="#007700">[</font><font color="#DD0000">'company'</font><font color="#007700">] as </font><font color="#0000BB">$company</font><font color="#007700">) {<br /> echo </font><font color="#DD0000">"Company obtained from the database has name = " </font><font color="#007700">. <br /> </font><font color="#0000BB">$company</font><font color="#007700">[</font><font color="#DD0000">'name'</font><font color="#007700">] . </font><font color="#DD0000">" and id " </font><font color="#007700">. </font><font color="#0000BB">$company</font><font color="#007700">[</font><font color="#DD0000">'id'</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.sdo-das-relational-createrootdataobject.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.sdo-das-relational-executequery.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >SDO_DAS_Relational::createRootDataObject</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.sdo.das.rel.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >SDO_DAS_Relational::executeQuery</TD ></TR ></TABLE ></DIV ></BODY ></HTML >