Sophie

Sophie

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

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_fetch_array</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_escape_string"
HREF="function.mysql-escape-string.html"><LINK
REL="NEXT"
TITLE="mysql_fetch_assoc"
HREF="function.mysql-fetch-assoc.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-escape-string.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-fetch-assoc.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.mysql-fetch-array"
></A
>mysql_fetch_array</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN130082"
></A
><P
>    (PHP 3, PHP 4, PHP 5)</P
>mysql_fetch_array&nbsp;--&nbsp;Obtém uma linha como uma matriz associativa, uma matriz numérica, ou ambas</DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN130085"
></A
><H2
>Descrição</H2
>array <B
CLASS="methodname"
>mysql_fetch_array</B
> ( resource result [, int result_type] )<BR
></BR
><P
>&#13;   Retorna uma matriz que corresponde a linha obtida
   e move o ponteiro interno dos dados adiante.
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN130097"
></A
><H2
>Parâmetros</H2
><P
>&#13;   <P
></P
><DIV
CLASS="variablelist"
><DL
><DT
><CODE
CLASS="parameter"
>&#13; result</CODE
></DT
><DD
><P
>O resulatado <A
HREF="language.types.resource.html"
><B
CLASS="type"
>resource</B
></A
> que
 esta sendo avaliado. Este resultado vem de uma chamada a
 <A
HREF="function.mysql-query.html"
><B
CLASS="function"
>mysql_query()</B
></A
>.</P
></DD
><DT
><CODE
CLASS="parameter"
>result_type</CODE
></DT
><DD
><P
>&#13;       O tipo de array que deve ser obtida. é uma constante e pode
       ter os seguintes valores: <TT
CLASS="constant"
><B
>MYSQL_ASSOC</B
></TT
>, 
       <TT
CLASS="constant"
><B
>MYSQL_NUM</B
></TT
>, e o valor padrão de  
       <TT
CLASS="constant"
><B
>MYSQL_BOTH</B
></TT
>.
      </P
></DD
></DL
></DIV
>
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN130116"
></A
><H2
>Valores de retornado</H2
><P
>&#13;   Retorna uma array que corresponde a linha obtida, ou <TT
CLASS="constant"
><B
>FALSE</B
></TT
>
   se não houver mais linhas. O tipo da array retornada depende de
   como <CODE
CLASS="parameter"
>result_type</CODE
> esta definido. Usando 
   <TT
CLASS="constant"
><B
>MYSQL_BOTH</B
></TT
> (padrão), você terá um array com ambos os
   índices, numérico e associativo. Usando <TT
CLASS="constant"
><B
>MYSQL_ASSOC</B
></TT
>, você
   tem apenas os índices associativos (como <A
HREF="function.mysql-fetch-assoc.html"
><B
CLASS="function"
>mysql_fetch_assoc()</B
></A
> 
   funciona), usando <TT
CLASS="constant"
><B
>MYSQL_NUM</B
></TT
>, você tem apenas os índices numéricos 
   (como <A
HREF="function.mysql-fetch-row.html"
><B
CLASS="function"
>mysql_fetch_row()</B
></A
> funciona).
  </P
><P
>&#13;   Se duas ou mais colunas do resultado tiverem os mesmos nomes de campos,
   a ultima coluna terá precedencia. Para acessar a(s) outra(s) coluna(s)
   com o mesmo nome, você deverá usar o índice numérico da coluna ou fazer
   um alias para a coluna. Para colunas com alias, você não pode acessar
   os conteúdos com o nome original da coluna.
  </P
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN130127"
></A
><H2
>Exemplos</H2
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN130130"
></A
><P
><B
>Exemplo 1. Consulta com nomes de campos duplicados usando alias</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="sql"
>SELECT table1.field AS foo, table2.field AS bar FROM table1, table2</PRE
></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="AEN130134"
></A
><P
><B
>Exemplo 2. <B
CLASS="function"
>mysql_fetch_array()</B
> com <TT
CLASS="constant"
><B
>MYSQL_NUM</B
></TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />mysql_connect</font><font color="#007700">(</font><font color="#DD0000">"localhost"</font><font color="#007700">, </font><font color="#DD0000">"mysql_user"</font><font color="#007700">, </font><font color="#DD0000">"mysql_password"</font><font color="#007700">) or<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#DD0000">"Não foi possível conectar: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br /></font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydb"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">"SELECT id, name FROM mytable"</font><font color="#007700">);<br /><br />while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_array</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">, </font><font color="#0000BB">MYSQL_NUM</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"ID: %s&nbsp;&nbsp;Name: %s"</font><font color="#007700">, </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]);&nbsp;&nbsp;<br />}<br /><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
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN130140"
></A
><P
><B
>Exemplo 3. <B
CLASS="function"
>mysql_fetch_array()</B
> com <TT
CLASS="constant"
><B
>MYSQL_ASSOC</B
></TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />mysql_connect</font><font color="#007700">(</font><font color="#DD0000">"localhost"</font><font color="#007700">, </font><font color="#DD0000">"mysql_user"</font><font color="#007700">, </font><font color="#DD0000">"mysql_password"</font><font color="#007700">) or<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#DD0000">"Não foi possível conectar: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br /></font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydb"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">"SELECT id, name FROM mytable"</font><font color="#007700">);<br /><br />while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_array</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">, </font><font color="#0000BB">MYSQL_ASSOC</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"ID: %s&nbsp;&nbsp;Name: %s"</font><font color="#007700">, </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"id"</font><font color="#007700">], </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"name"</font><font color="#007700">]);<br />}<br /><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
><P
>&#13;   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN130146"
></A
><P
><B
>Exemplo 4. <B
CLASS="function"
>mysql_fetch_array()</B
> com <TT
CLASS="constant"
><B
>MYSQL_BOTH</B
></TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />mysql_connect</font><font color="#007700">(</font><font color="#DD0000">"localhost"</font><font color="#007700">, </font><font color="#DD0000">"mysql_user"</font><font color="#007700">, </font><font color="#DD0000">"mysql_password"</font><font color="#007700">) or<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</font><font color="#DD0000">"Não foi possível conectar: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br /></font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydb"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$result </font><font color="#007700">= </font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">"SELECT id, name FROM mytable"</font><font color="#007700">);<br /><br />while (</font><font color="#0000BB">$row </font><font color="#007700">= </font><font color="#0000BB">mysql_fetch_array</font><font color="#007700">(</font><font color="#0000BB">$result</font><font color="#007700">, </font><font color="#0000BB">MYSQL_BOTH</font><font color="#007700">)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">printf </font><font color="#007700">(</font><font color="#DD0000">"ID: %s&nbsp;&nbsp;Name: %s"</font><font color="#007700">, </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">], </font><font color="#0000BB">$row</font><font color="#007700">[</font><font color="#DD0000">"name"</font><font color="#007700">]);<br />}<br /><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="AEN130151"
></A
><H2
>Notas</H2
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Performance: </B
>
    Uma coisa importante para notar que usar 
    <B
CLASS="function"
>mysql_fetch_array()</B
> <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>não 
    é significativamente</I
></SPAN
> mais lento do que usar
    <A
HREF="function.mysql-fetch-row.html"
><B
CLASS="function"
>mysql_fetch_row()</B
></A
>, enquanto provê
    um valor agregado significante.
   </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>Nomes de campos nesta função
 <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>diferenciam maiúsculas e minusculas</I
></SPAN
>.</P
></BLOCKQUOTE
></DIV
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Nota: </B
>Esta função assimila campos NULL para o
 o <TT
CLASS="constant"
><B
>NULL</B
></TT
> do PHP.</P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN130165"
></A
><H2
>Veja também</H2
><P
>&#13;   <P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="function.mysql-fetch-row.html"
><B
CLASS="function"
>mysql_fetch_row()</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-data-seek.html"
><B
CLASS="function"
>mysql_data_seek()</B
></A
></TD
></TR
><TR
><TD
><A
HREF="function.mysql-query.html"
><B
CLASS="function"
>mysql_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-escape-string.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-fetch-assoc.html"
ACCESSKEY="N"
>Próxima</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>mysql_escape_string</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_fetch_assoc</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>