将数据库记录存储到数组中 [英] Storing database records into array

查看:51
本文介绍了将数据库记录存储到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个数组,用于保存使用 SELECT 语句查询从数据库中检索到的记录.

I would want to create an array that will hold records retrieved from a database using a query of SELECT statement.

要检索的记录有多个字段,例如姓氏、名字、mi 和另外 20 个字段.编写此函数的最佳方法是什么?

The records to be retrieved have multiple fields such as lastname, firstname, mi and 20 more fields. What would be the best approach on coding this function?

好吧,我遵循了囚犯在下面给出的内容……下一个问题是我如何使用查询来搜索这种数组?例如我想搜索用户名..

alright i have followed what prisoner have given below.. the next question is how do i search through this kind of array using queries? for example i want to search for a username..

推荐答案

<?php

// run query
$query = mysql_query("SELECT * FROM table");

// set array
$array = array();

// look through query
while($row = mysql_fetch_assoc($query)){

  // add each row returned into an array
  $array[] = $row;

  // OR just echo the data:
  echo $row['username']; // etc

}

// debug:
print_r($array); // show all array data
echo $array[0]['username']; // print the first rows username

这篇关于将数据库记录存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆