dongqun1656 2013-11-06 23:52
浏览 37
已采纳

如何手动从PHP中获取Mysql记录? [关闭]

I am having table with two columns Symbol and weight. I am using this code

$table=mysql_fetch_array($result);
echo count($table);
echo "<pre>";
print_r($table);
echo "</pre>";

Out of the Above Code is

4
Array
(
    [0] => x1
    [symbol] => x1
    [1] => 0.50
    [weight] => 0.50
)

My table contains 4 entries. But it is showing only one. i want to see all the entries, can you tell how to access them manually. I know i can access them using.

while($table=mysql_fetch_array($result)
{
echo "<br/>".$table['symbol']."\t".$table['weight'];
}

But I want to do this manually. Because later i want to perform some manual operation on this array.

Content of my Table is

Symbol | Weight
    X1 | .50
    X2 | .25
    X3 | .20
    X4 | .10

By manually i mean, i will fetch the last two record without traversing the whole array(i.e with the help of while) then add the weight of these last two records.

For Example : Here my table have 4 entries and last two records are X3 and X4 with weight .20 and .10. I will replace these two records with the new single records that is X33 with weight .30. and my new table will look like

Symbol | Weight
    X1 | .50
    X2 | .25
    X33| .30
  • 写回答

1条回答 默认 最新

  • dpswo40440 2013-11-06 23:59
    关注

    You have two options, throw in a WHERE clause or do a loop until you get all the data and store it in your own array.

    while($table=mysql_fetch_array($result)
    {
        $data[]=$table;
    }
    

    With your array, you can then access it manually however you would like.

    The best option is to use mysqli or PDO since mysql is deprecated and will be removed in the future. I know mysqli result object has the fetch_all function to get an array of objects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Java中消息和缓存如何使用
  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路