发布于 2011-12-01 20:12:27
文档在这一点上很清楚,你看过吗?
mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )返回一个字符串数组,该数组对应于获取的行,如果没有更多行,则返回FALSE。返回数组的类型取决于如何定义result_type。通过使用MYSQL_BOTH (默认),您将得到一个具有关联索引和数字索引的数组。使用MYSQL_ASSOC,您只获得关联索引(如mysql_fetch_assoc()工作),通过使用MYSQL_NUM,您只获得数字索引(如mysql_fetch_row()工作)。
mysql_fetch_row ( resource $result )返回与所取行对应的字符串的数字数组,如果没有更多行,则返回FALSE。 mysql_fetch_row()从与指定结果标识符关联的结果中获取一行数据。以数组的形式返回行。每个结果列都存储在数组偏移量中,从偏移量0.开始。
总而言之
mysql_fetch_array( $result, MYSQL_ASSOC ) = mysql_fetch_assoc( $result ) mysql_fetch_array( $result, MYSQL_NUM ) = mysql_fetch_row( $result )
和
mysql_fetch_array ( $result ) = mysql_fetch_assoc( $result ) + mysql_fetch_row( $result )
发布于 2013-03-29 11:55:23
许多php编程新手对mysql_fetch_array()、mysql_fetch_row()、mysql_fetch_assoc()和mysql_fetch_object()函数感到困惑,但所有这些函数都执行类似的过程。
让我们为明确的示例创建一个表“tb”,其中包含三个字段“id”、“username”和“password”。
表: tb
在表中插入一个新行,id值为1,用户名值为tobby,密码为78美元。

db.php
<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?>mysql_fetch_row()
将结果行提取为数字数组
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_row($query);
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>结果
1本书$78$2
mysql_fetch_object()
作为对象获取结果行
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_object($query);
echo $row->id;
echo $row->username;
echo $row->password;
?>
</html>结果
1本书$78$2
mysql_fetch_assoc()
将结果行提取为关联数组
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_assoc($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
?>
</html> 结果
1本书$78$2
mysql_fetch_array()
将结果行提取为关联数组、数字数组以及由关联数组和数字数组获取的结果行。
<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_array($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
<span style="color: #993300;">/* here both associative array and numeric array will work. */</span>
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>结果
1本书$78$2
发布于 2014-05-31 13:48:18
mysql_fetch_object{row/array/assoc}()函数以其各自的格式收集第一个匹配记录,并相应地进行检索。
con.php
<?php
$host = $_GET['host'];
$username = $_GET['username'];
$pass = $_GET['pass'];
$database = $_GET['database'];
$connect=new connect($host,$username,$pass,$database);
class connect{
function __construct($host,$user,$password,$db_name){
mysql_connect($host,$user,$password) or die("Connection error");
mysql_select_db($db_name);
$error=mysql_error();
if (!empty($error))
{
echo $error;
}
}
}
?>

index.php
<?php
$query=mysql_query("select * from category");
?>每个值如何在浏览器中转储数组
mysql_fetch_array
$row=mysql_fetch_array($query);
var_dump($row);输出:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string '1' (length=1)
'createdBy' => string '1' (length=1)
2 => string 'APTITUDE' (length=8)
'catName' => string 'APTITUDE' (length=8)
3 => string 'APTITUDE' (length=8)
'description' => string 'APTITUDE' (length=8)
4 => string '1' (length=1)
'status' => string '1' (length=1)mysql_fetch_row
$row=mysql_fetch_row($query);
var_dump($row);输出:
array
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'APTITUDE' (length=8)
3 => string 'APTITUDE' (length=8)
4 => string '1' (length=1)mysql_fetch_assoc
$row=mysql_fetch_assoc($query);
var_dump($row);输出:
array
'id' => string '1' (length=1)
'createdBy' => string '1' (length=1)
'catName' => string 'APTITUDE' (length=8)
'description' => string 'APTITUDE' (length=8)
'status' => string '1' (length=1)mysql_fetch_object
$row=mysql_fetch_object($query);
var_dump($row);输出:
object(stdClass)[2]
public 'id' => string '1' (length=1)
public 'createdBy' => string '1' (length=1)
public 'catName' => string 'APTITUDE' (length=8)
public 'description' => string 'APTITUDE' (length=8)
public 'status' => string '1' (length=1)Rest @Gaurang提供了如何在代码和其他活动中使用out。
https://stackoverflow.com/questions/8347565
复制相似问题