search
Homephp教程php手册[Transfer] The difference between mysql_fetch_row, mysql_fetch_array and mysql_fetch_assoc

<span style="color: #000000;">php
</span><span style="color: #800080;">$link </span>= <span style="color: #008080;">mysql_connect</span>('localhost', 'root', <span style="color: #000000;">”);
</span><span style="color: #008080;">mysql_select_db</span>('abc', <span style="color: #800080;">$link</span><span style="color: #000000;">);
</span><span style="color: #800080;">$sql</span> = “select *<span style="color: #000000;"> from book”;
</span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">))
{
  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['cid'].'::'.<span style="color: #800080;">$row</span>[1].'<br>'<span style="color: #000000;">;
}
</span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">))
{
  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['cid'].'::'.<span style="color: #800080;">$row</span>[1].'<br>'<span style="color: #000000;">;
}
</span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_object</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">))
{
  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>->cid.'::'.<span style="color: #800080;">$row</span>->title.”<br><span style="color: #000000;">”;
}
</span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_assoc</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">))
{
  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['cid'].'::'.<span style="color: #800080;">$row</span>[1].'<br>'<span style="color: #000000;">;
}
</span>?>

Analysis:
mysql_fetch_row, this function takes a row from the result set as enumeration data, obtains a row of data from the result set associated with the specified result identifier and returns it as an array. Each result column is stored in a cell of the array, starting at offset 0. Note that the offset here starts from 0, which means that you cannot use the field name to get the value, you can only use the index to get the value, so the following code cannot get the value:
 while($row = mysql_fetch_row($res) ){
  echo $row['cid'].'::'.$row[1].";
  } //$row['cid'] here cannot get the value.
mysql_fetch_array, obtained from the result set A row is used as an associative array, or a numeric array, or both. In addition to storing the data in the array as a numerical index, the data can also be stored as an associative index, using the field name as the key name. That is to say, he gets. The result is like an array and can be retrieved by key or index, so
 while($row = mysql_fetch_array($res)){
  echo $row['cid'].'::'.$row[1]. ”;
 }//Here $row['cid'], $row[1] can get the corresponding value.
mysql_fetch_object, as the name suggests, fetches a row from the result set as an object and uses the field name as an attribute. So the only way to get the value
  while($row = mysql_fetch_object($res)){
   echo $row->cid.'::'.$row->title."";
 }
mysql_fetch_assoc, from Get a row from the result set as an associative array, which means that this function cannot use the index to get the value like mysql_fetch_row, but can only use the field name to get the value, so
  while($row = mysql_fetch_assoc($res)){
  echo $row[ 'cid'].'::'.$row[1].”;
 } //$row[1] cannot get the value like this
Additional point:
The mysql_fetch_array function is defined like this: array mysql_fetch_array (resource result [, int result_type]), returns an array generated based on the rows obtained from the result set, or FALSE if there are no more rows. The optional second parameter result_type in
mysql_fetch_array() is a constant and can accept the following values. : MYSQL_ASSOC, MYSQL_NUM and MYSQL_BOTH. Among them:
1. mysql_fetch_assoc($result) == mysql_fetch_array($result, MYSQL_ASSOC);
2. mysql_fetch_row($result) == mysql_fetch_array($result, MYSQL_NUM);
So mysql_fetch_array() To a certain extent, the function can be regarded as a collection of mysql_fetch_row() and mysql_fetch_assoc(). In addition, mysql_fetch_array() also has the MYSQL_BOTH parameter, which will get an array that contains both association and numeric index.
Let’s say $row = $. db->fetch_array($query);
$db is the human database operation class, $db->fetch_array($query), fetch_array($query) is the method in that db class, $row = $db-> ;fetch_array($query) means to get a row of records in the database from the record set $query.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor