mysql_fetch_array如何显示列的单个值

时间:2014-02-24 20:06:10

标签: php

我有查询提取8行,我希望能够通过单列而不是整行访问结果。例如,我想从第7行显示Deviceid。我可以显示列的所有值,但我无法弄清楚如何只获取一个值。我是PHP新手所以请耐心等待,谢谢你的帮助。 这段代码:

 <?php
 $con = mysql_connect("uimcswpro", "testmysql", "test123") or die('Could not connect to server');
 mysql_select_db("storage", $con) or die('Sorry, could not connect to the database');

 $query = "Select distinct a.Deviceid as Deviceid, c.Time as Time, b.devicehostname as devicehostname, ElementName, PoolID,
 TotalManagedSpace, RemainingManagedSpace,
 TotalDiskCapacity, HotspareCapacity, UnassignedStorageCapacity,
 AssignedStorageCapacity 
 from tt_cim_storage_pool a, devices b, ent_san_storage_current c
 where a.Deviceid = b.Deviceid
 and (type = 'Unified Pool' or type = 'RAID Group' or type is NULL)
 and c.time = (select max(c.Time) from tt_cim_storage_pool where deviceid=
 (select deviceid from devices where devicehostname='UIH_8100_L14' and retire=0))
 and devicehostname='UIH_8100_L14';";

 $result = mysql_query($query,$con);
 $num_rows = mysql_num_rows($result);
 echo "$num_rows Rows\n";
 echo "<br/>";

 while ($row=mysql_fetch_array($result)) {
 echo $row[0]. " - ".$row[1]. " - ".$row['devicehostname']. " - ".$row['ElementName'].   " - ".$row['PoolID']
 . " - ".$row['TotalDiskCapacity'];
 echo "<br/>";
 }

 mysql_close($con);
 ?>   

1 个答案:

答案 0 :(得分:0)

您可以使用LIMIT关键字通过sql选择所需的行,或者简单检查当前行的编号:

$rowNo = 0;
while ($row=mysql_fetch_array($result)) {
 $rowNo++;
 if ($rowNo == 7) {
   echo $row[0]. " - ".$row[1]. " - ".$row['devicehostname']. " - ".$row['ElementName'].   " - ".$row['PoolID']
 . " - ".$row['TotalDiskCapacity'];
   echo "<br/>";
 }
}

您还应该考虑使用像mysqli

这样的现代数据库界面