id, age
-- -----
1 2
2 5
3 6
4 7
5 8
我想从年龄中获得一个值。我的代码的结果是25678.我怎么才能得到一个值。
$query= "SELECT age FROM person";
$result= mysql_query($query) or die ("Query error: " . mysql_error());
while($row = mysql_fetch_array($result))
{
$age= $row['age'];
echo $age; // the result will be 25678
// i want to get a value from the result ex = 5 or 6
//I try with call the index like my code bellow but It's not working
echo $age[1];
echo $age[2];
}
有人能帮帮我......
答案 0 :(得分:0)
while($row = mysql_fetch_array($result)) {
$age = $row[0];
echo $age."<br />";
}
while循环将一直运行到$ result数组的结尾。在每个循环中,它将回显当前行。
答案 1 :(得分:0)
好吧,如果你想要其中一个年龄段。保存数组中的所有行并获取您选择的索引。
示例:
$query= "SELECT age FROM person";
$result= mysql_query($query) or die ("Query error: " . mysql_error());
$ageArray;
$i=0;
while($row = mysql_fetch_array($result))
{
//$age= $row['age'];
$ageArray[$i++]=$row['age'];
}
echo $ageArray[1];
我希望这就是你想要的......
答案 2 :(得分:0)
试试这个
$query= "SELECT age FROM person";
$result= mysql_query($query) or die ("Query error: " . mysql_error());
$array_r;
while($row = mysql_fetch_array($result))
{
array_push($array_r,$row['age']);
}
echo '<pre/>';
print_r($array_r);
答案 3 :(得分:0)
我认为你的情况有两件事:
<强> 1。通过传递条件查询:
由于您只想获得一个值,因此您必须根据id
字段传递条件,这样您才能获得单个值
$query= "SELECT age FROM person where id={$id}"; //here $id may be any numeric value which you have specified
$result= mysql_query($query) or die ("Query error: " . mysql_error());
$age = mysql_fetch_assoc($result);
if(count($age)){
echo $age[0]['age']; //this will print only single value based on condition you have specified in SQL query
}
<强> 2。通过将结果(年龄)分配给数组然后打印
$query= "SELECT age FROM person";
$result= mysql_query($query) or die ("Query error: " . mysql_error());
$age = array();
while($row = mysql_fetch_array($result))
{
$age[] = $row['age'];
}
echo "<pre>";
print_r($age);
echo $age[1]; //this will print 5
echo $age[2]; //this will print 6
答案 4 :(得分:0)
试试这个
$query= "SELECT age FROM person";
$result= mysql_query($query) or die ("Query error: " . mysql_error());
//take age in an array
$age=array();
while($row = mysql_fetch_array($result))
{
array_push($age, $row['age']);
echo $row['age']; // the result will be 25678
// i want to get a value from the result ex = 5 or 6
//I try with call the index like my code bellow but It's not working
echo $age[1];
echo $age[2]; // now u will get desired result by calling index of $age array
}