<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >PDO->exec()</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="PDO Functions" HREF="ref.pdo.html"><LINK REL="PREVIOUS" TITLE="PDO->errorInfo()" HREF="function.pdo-errorinfo.html"><LINK REL="NEXT" TITLE="PDO->getAttribute()" HREF="function.pdo-getattribute.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.pdo-errorinfo.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.pdo-getattribute.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.PDO-exec" ></A >PDO->exec()</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN172126" ></A ><P > (no version information, might be only in CVS)</P >PDO->exec() -- Execute an SQL statement and return the number of affected rows </DIV ><DIV CLASS="refsect1" ><A NAME="AEN172129" ></A ><H2 >Descrição</H2 >class <B CLASS="classname" >PDO</B > { <BR ></BR >int <B CLASS="methodname" >exec</B > ( string statement )<BR ></BR >}<P > <A HREF="function.pdo-exec.html" >PDO->exec()</A > executes an SQL statement in a single function call, returning the number of rows affected by the statement. </P ><P > <A HREF="function.pdo-exec.html" >PDO->exec()</A > does not return results from a SELECT statement. For a SELECT statement that you only need to issue once during your program, consider issuing <A HREF="function.pdo-query.html" >PDO->query()</A >. For a statement that you need to issue multiple times, prepare a PDOStatement object with <A HREF="function.pdo-prepare.html" >PDO->prepare()</A > and issue the statement with <A HREF="function.pdostatement-execute.html" >PDOStatement->execute()</A >. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172147" ></A ><H2 >Parâmetros</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >statement</CODE ></DT ><DD ><P > The SQL statement to prepare and execute. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172156" ></A ><H2 >Valores de retornado</H2 ><P > <A HREF="function.pdo-exec.html" >PDO->exec()</A > returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, <A HREF="function.pdo-exec.html" >PDO->exec()</A > returns <TT CLASS="literal" >0</TT >. </P ><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 pode retornar o booleano <TT CLASS="constant" ><B >FALSE</B ></TT >, mas também pode retornar um valor não-booleano que pode ser avaliado como <TT CLASS="constant" ><B >FALSE</B ></TT >, como <TT CLASS="literal" >0</TT > ou "". Leia a seção em <A HREF="language.types.boolean.html" >Booleanos</A > para maiores informações. Utilize o <A HREF="language.operators.comparison.html" >operador === </A > para testar o valor retornado por esta função.</P ></TD ></TR ></TABLE ></DIV ><P > The following example incorrectly relies on the return value of <A HREF="function.pdo-exec.html" >PDO->exec()</A >, wherein a statement that affected 0 rows results in a call to <A HREF="function.die.html" ><B CLASS="function" >die()</B ></A >: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$db</font><font color="#007700">-></font><font color="#0000BB">exec</font><font color="#007700">() or die(</font><font color="#0000BB">$db</font><font color="#007700">-></font><font color="#0000BB">errorInfo</font><font color="#007700">());<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172173" ></A ><H2 >Exemplos</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN172176" ></A ><P ><B >Exemplo 1. Issuing a DELETE statement</B ></P ><P > Count the number of rows deleted by a DELETE statement with no WHERE clause. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$dbh </font><font color="#007700">= new </font><font color="#0000BB">PDO</font><font color="#007700">(</font><font color="#DD0000">'odbc:sample'</font><font color="#007700">, </font><font color="#DD0000">'db2inst1'</font><font color="#007700">, </font><font color="#DD0000">'ibmdb2'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Delete all rows from the FRUIT table */<br /></font><font color="#0000BB">$count </font><font color="#007700">= </font><font color="#0000BB">$dbh</font><font color="#007700">-></font><font color="#0000BB">exec</font><font color="#007700">(</font><font color="#DD0000">"DELETE FROM fruit WHERE colour = 'red'"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Return number of rows that were deleted */<br /></font><font color="#007700">print(</font><font color="#DD0000">"Deleted $count rows.</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Deleted 1 rows.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN172182" ></A ><H2 >Veja também</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.pdo-prepare.html" >PDO->prepare()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdo-query.html" >PDO->query()</A ></TD ></TR ><TR ><TD ><A HREF="function.pdostatement-execute.html" >PDOStatement->execute()</A ></TD ></TR ></TBODY ></TABLE ><P ></P > </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.pdo-errorinfo.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.pdo-getattribute.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >PDO->errorInfo()</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.pdo.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >PDO->getAttribute()</TD ></TR ></TABLE ></DIV ></BODY ></HTML >