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

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

问题描述

我想创建一个数组,将持有使用SELECT语句的查询从数据库中检索的记录。

要检索的记录具有多个字段,如姓氏,名字,MI和20多个领域。什么是对编码此功能的最佳方法?

好吧,我都遵循哪些囚犯已经给下面..接下来的问题是我如何通过使用查询这种阵列的搜索?例如我要搜索用户名。


解决方案

 < PHP//运行查询
$查询=的mysql_query(SELECT * FROM表);//设置阵列
$阵列=阵列();//浏览查询
而($行= mysql_fetch_assoc($查询)){  //添加每一行返回到一个数组
  $数组[] = $行;  //或者只是呼应的数据:
  回声$行[用户名]; //等}//调试:
的print_r($数组); //显示所有阵列数据
回声$阵列[0] [用户名]; //打印第一行的用户名

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

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天全站免登陆