douliao5467 2011-10-02 17:05
浏览 64
已采纳

根据索引回显查询结果

This is pretty basic but I can't seem to get it to work

I have this query

$people = "SELECT name FROM people";
$people = mysql_query($people) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);

I can echo the first result of this query with

<?php echo $row_people['name']; ?>

I could also create a loop and echo all the results.

But I really want to echo the results individually based on its index.

I have tried this, but it does not work.

<?php echo $row_people['name'][2]; ?>

Thanks for your help.

  • 写回答

3条回答 默认 最新

  • dongniechi7825 2011-10-02 17:08
    关注

    You can fetch them by their index using a WHERE clause.

    $people = sprintf("SELECT name FROM people WHERE index='%d'", $index);
    

    If you want to query all rows, you could store them into an array while looping over them:

    $people = "SELECT name FROM people";
    $people = mysql_query($people) or die(mysql_error());
    $totalRows_people = mysql_num_rows($people);
    $rows_people = array();
    while($row_people = mysql_fetch_assoc($people))
    {
        $rows_people[] = $row_people;
    }
    

    You might want to add the primary key to the returned fields and use it as the array index probably.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思