Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 703d980c580707c382b4e43e25965bc5 > files > 2709

php-manual-pt_BR-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>mysql_query</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="Funções do MySQL"
HREF="ref.mysql.html"><LINK
REL="PREVIOUS"
TITLE="mysql_ping"
HREF="function.mysql-ping.html"><LINK
REL="NEXT"
TITLE="mysql_real_escape_string"
HREF="function.mysql-real-escape-string.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.mysql-ping.html"
ACCESSKEY="P"
>Anterior</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.mysql-real-escape-string.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.mysql-query"
></A
>mysql_query</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN131751"
></A
><P
>    (PHP 3, PHP 4, PHP 5)</P
>mysql_query&nbsp;--&nbsp;Envia uma consulta MySQL</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN131754"
></A
><H2
>Descrição</H2
>resource <B
CLASS="methodname"
>mysql_query</B
> ( string query [, resource link_identifier] )<BR
></BR
><P
>&#13;     <B
CLASS="function"
>mysql_query()</B
> envia uma consulta (para o banco de dados
     atualmente ativo no servidor associado ao
     <CODE
CLASS="parameter"
>link_identifier</CODE
> especificado).
    </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN131768"
></A
><H2
>Parâmetros</H2
><P
>&#13;     <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><CODE
CLASS="parameter"
>query</CODE
></DT
><DD
><P
>&#13;         A consulta SQL.
        </P
><P
>&#13;         A consulta não deve terminar com um ponto e vírgula.
        </P
></DD
><DT
><CODE
CLASS="parameter"
>&#13; link_identifier</CODE
></DT
><DD
><P
>A conexão MySQL. se o identificador
 de conexão não for especificado, a ultima conexão aberta por
 <A
HREF="function.mysql-connect.html"
><B
CLASS="function"
>mysql_connect()</B
></A
> é assumida. Se uma conexão assim não for encontrada,
 ela irá tentar criar uma como se a função <A
HREF="function.mysql-connect.html"
><B
CLASS="function"
>mysql_connect()</B
></A
> fosse chamada
 sem argumentos. Se uma conexão não for encontrada ou estabelecida, um
 aviso de nível <TT
CLASS="constant"
><B
>E_WARNING</B
></TT
> é gerado.</P
></DD
></DL
></DIV
>
    </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN131786"
></A
><H2
>Valores de retornado</H2
><P
>&#13;     Para comandos SELECT, SHOW, DESCRIBE ou EXPLAIN,
     <B
CLASS="function"
>mysql_query()</B
> 
     retorna um <A
HREF="language.types.resource.html"
><B
CLASS="type"
>resource</B
></A
> em caso de sucesso, ou <TT
CLASS="constant"
><B
>FALSE</B
></TT
> em
     caso de falha.
    </P
><P
>&#13;     Para outros tipos de consultas SQL, UPDATE, DELETE, DROP, etc,
     <B
CLASS="function"
>mysql_query()</B
> retorna <TT
CLASS="constant"
><B
>TRUE</B
></TT
> em caso de
     sucesso ou <TT
CLASS="constant"
><B
>FALSE</B
></TT
> em caso de erro.
    </P
><P
>&#13;     O recurso de resultado retornado pode ser passado para
     <A
HREF="function.mysql-fetch-array.html"
><B
CLASS="function"
>mysql_fetch_array()</B
></A
>, e outras 
     funções para manipular tabelas de resultados, para acessar os dados retornados.
    </P
><P
>&#13;     Use <A
HREF="function.mysql-num-rows.html"
><B
CLASS="function"
>mysql_num_rows()</B
></A
> para obter quantas linhas foram retornadas
     para um comando SELECT ou
     <A
HREF="function.mysql-affected-rows.html"
><B
CLASS="function"
>mysql_affected_rows()</B
></A
> para obter quantas
     linhas foram afetadas por um comando DELETE, INSERT, REPLACE,
     ou UPDATE.
    </P
><P
>&#13;     <B
CLASS="function"
>mysql_query()</B
> irá também falhar e retornar <TT
CLASS="constant"
><B
>FALSE</B
></TT
>
     se o usuário não tiver permissões para acessar a tabela(s) referenciadas
     pela consulta.
    </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN131804"
></A
><H2
>Exemplos</H2
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN131807"
></A
><P
><B
>Exemplo 1. Consulta Inválida</B
></P
><P
>&#13;       A seguinte consulta é sintaticamente inválida, assim
       <B
CLASS="function"
>mysql_query()</B
> falha e retorna <TT
CLASS="constant"
><B
>FALSE</B
></TT
>.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">'SELECT * WHERE 1=1'</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$result</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#DD0000">'Invalid query: ' </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN131814"
></A
><P
><B
>Exemplo 2. Consulta válida</B
></P
><P
>&#13;       A seguinte consulta é válida, assim <B
CLASS="function"
>mysql_query()</B
>
       retorna um <A
HREF="language.types.resource.html"
><B
CLASS="type"
>resource</B
></A
>.
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// This could be supplied by a user, for example<br /></font><font color="#0000BB">$firstname </font><font color="#007700">= </font><font color="#DD0000">'fred'</font><font color="#007700">;<br /></font><font color="#0000BB">$lastname&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">'fox'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Formulate Query<br />// This is the best way to perform a SQL query<br />// For more examples, see mysql_real_escape_string()<br /></font><font color="#0000BB">$query </font><font color="#007700">= </font><font color="#0000BB">sprintf</font><font color="#007700">(</font><font color="#DD0000">"SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mysql_real_escape_string</font><font color="#007700">(</font><font color="#0000BB">$firstname</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">mysql_real_escape_string</font><font color="#007700">(</font><font color="#0000BB">$lastname</font><font color="#007700">));<br /><br /></font><font color="#FF8000">// Perform Query<br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#0000BB">$query</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Check result<br />// This shows the actual query sent to MySQL, and the error. Useful for debugging.<br /></font><font color="#007700">if (!</font><font color="#0000BB">$result</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$message&nbsp;&nbsp;</font><font color="#007700">= </font><font color="#DD0000">'Invalid query: ' </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">() . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$message </font><font color="#007700">.= </font><font color="#DD0000">'Whole query: ' </font><font color="#007700">. </font><font color="#0000BB">$query</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#0000BB">$message</font><font color="#007700">);<br />}<br /><br /></font><font color="#FF8000">// Use result<br />// Attempting to print $result won't allow access to information in the resource<br />// One of the mysql result functions must be used<br />// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.<br /></font><font color="#007700">while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_assoc</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'firstname'</font><font color="#007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'lastname'</font><font color="#007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'address'</font><font color="#007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">'age'</font><font color="#007700">];<br />}<br /><br /></font><font color="#FF8000">// Free the resources associated with the result set<br />// This is done automatically at the end of the script<br /></font><font color="#0000BB">mysql_free_result</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN131820"
></A
><H2
>Veja também</H2
><P
>&#13;     <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="function.mysql-connect.html"
><B
CLASS="function"
>mysql_connect()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.mysql-error.html"
><B
CLASS="function"
>mysql_error()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.mysql-real-escape-string.html"
><B
CLASS="function"
>mysql_real_escape_string()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.mysql-result.html"
><B
CLASS="function"
>mysql_result()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.mysql-fetch-assoc.html"
><B
CLASS="function"
>mysql_fetch_assoc()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.mysql-unbuffered-query.html"
><B
CLASS="function"
>mysql_unbuffered_query()</B
></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.mysql-ping.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.mysql-real-escape-string.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>mysql_ping</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.mysql.html"
ACCESSKEY="U"
>Acima</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>mysql_real_escape_string</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>