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

[Transfer] The difference between mysql_fetch_row, mysql_fetch_array and mysql_fetch_assoc

WBOY
WBOYOriginal
2016-09-29 09:19:021127browse
<?<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;">&rdquo;);
</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> = &ldquo;select *<span style="color: #000000;"> from book&rdquo;;
</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