您好,我对下面的一个表进行了查询,当我尝试获取每一列中的值时,它从第一列中为所有其他列返回相同的值。要详细说明
在我的数据库表中,我有以下内容:
owner_id = 21
pet_id = 1
name = fluffy
color = green
type = dog
sub_type = boxer
location = LA但是,每当我尝试访问一列时,它返回21,这是对应于该pet_id的owner_id列中的值。我不确定为什么会发生这种情况。
$query = sprintf("SELECT * FROM `petAttributes` where pet_id ='%d'",$p_id);
$result = performQuery($query);
$owner_id = stripslashes(mysql_result($result,"owner_id"));
$pet_id = stripslashes(mysql_result($result,"pet_id"));
$name = stripslashes(mysql_result($result,"name"));
$color = stripslashes(mysql_result($result,"color"));
$type = stripslashes(mysql_result($result,"type"));
$sub_type = stripslashes(mysql_result($result,"sub_type"));
$loc = stripslashes(mysql_result($result,"location"));我的环境信息
PHP版本5.2.14
MYSQL版本5.0.67
发布于 2010-10-13 17:33:11
我相信,如果使用mysql_result,在指定列之前,还必须指定行索引号(在本例中是第0行?)。
$name = stripslashes(mysql_result($result, 0, "name"));发布于 2010-10-13 17:32:45
参考http://php.net/manual/en/function.mysql-result.php mysql_result,它的参数如下:mysql_result($result,$rownumber,$fieldname or $fieldnumber)
这应该是可行的:
$query = sprintf("SELECT * FROM petAttributes where pet_id ='%d'",$p_id);
$result = performQuery($query);
$owner_id = stripslashes(mysql_result($result,0,"owner_id"));
$pet_id = stripslashes(mysql_result($result,0,"pet_id"));
$name = stripslashes(mysql_result($result,0,"name"));
$color = stripslashes(mysql_result($result,0,"color"));
$type = stripslashes(mysql_result($result,0,"type"));
$sub_type = stripslashes(mysql_result($result,0,"sub_type"));
$loc = stripslashes(mysql_result($result,0,"location"));顺便说一句,如果你取多个行,mysql_result的效率会变得非常低。然后,您应该使用mysql_fetch_row、mysql_fetch_array或mysql_fetch_assoc
发布于 2010-10-13 17:42:58
你也可以;
仅适用于第一行
$query = sprintf("SELECT * FROM petAttributes where pet_id ='%d'",$p_id);
$result = performQuery($query);
$row = mysql_fetch_array($result);
extract($row);或所有返回的行;
$query = sprintf("SELECT * FROM petAttributes where pet_id ='%d'",$p_id);
$result = performQuery($query);
while($row = mysql_fetch_array($result))
{
foreach ($row as $value) echo $value."<br>";
}https://stackoverflow.com/questions/3922284
复制相似问题