您好我对下面的表格进行了查询,当我尝试获取每列中的值时,它会从所有其他列的第一列返回相同的值。
详细说明
在我的数据库表中,我有以下内容:
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
答案 0 :(得分:1)
我相信如果您使用mysql_result,您还必须在指定列之前指定行索引号(在您的情况下为第0行?)。
$name = stripslashes(mysql_result($result, 0, "name"));
答案 1 :(得分:0)
引用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"));
如果您使用多行,BTW mysql_result的效率会非常低。 那你应该使用mysql_fetch_row,mysql_fetch_array或mysql_fetch_assoc
答案 2 :(得分:0)
你也可以;
仅限第一行
$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>";
}