
正文
php将数据存到数组中 php将数据存到数组中的方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
关于php获取数据库的内容存为数组的问题
php查询mysql数据库并将结果保存到数组php将数据存到数组中的方法。具体分析如下php将数据存到数组中:
主要用到了mysql_fetch_assoc函数
mysql_fetch_assoc语法如下:
?
1
array mysql_fetch_assoc (resource $Result_Set)
范例代码如下php将数据存到数组中:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
?php
$UserName = 'abc';
$Password = '1234';
$DbHandle = mysql_connect ('localhost', $UserName, $Password);
if (!$DbHandle) {
die 'No database connection could be established.';
}
$DBName = 'w3db;
if (!mysql_select_db ($DBName, $DbHandle)) {
die 'Database could not be selected.';
}
$Query = "SELECT ISBN, Title, Author FROM articles";
$articles = mysql_query ($Query, $DbHandle));
while ($Row = mysql_fetch_assoc ($articles)) {
echo "ISBN = $Row['ISBN']br /\n";
echo "Title = $Row['Title']br /\n";
echo "Author = $Row['Author']br /\n";
}
?
相关问答
Q1: 如何把php 查询结果存入数组
遍历数据表,把相应的数据放到数组中即可
例如:
?php
//定义一个数组,用于保存读取到的数据
$contents
=
array();
$query
=
mysql_query("select
*
from
table");
//遍历数据表
while($array
=
mysql_fetch_array($query)){
$contents[]
=
$array;
}
print_r($contents);
//然后循环数组,或者通过键名使用数组
foreach($contents
as
$value){
print_r($value);
}
echo
$contents[0]['字段名称'];
?
Q2: 用php如何获取数据库中的数据并按条件存入数组中?
$conn=mysql_connect(localhost,root,root) or die("could not connect mysql");
mysql_select_db(数据库,$conn);
$query="SELECT * FROM 表";
$result=mysql_query($query);
while($array=mysql_fetch_array($result)){
arr[]=[$array[1],$array[2]],[$array[2],$array[3]],[$array[3],$array[4]];
}
Q3: PHP怎样把得到的数据的值保存在一个数组里面
input 存在着2个方式 get 和post php将数据存到数组中,php将数据存到数组中你可以根据你的需求去选择。
现在很多框架都支持直接将post或者get到的数据作为整个数组保存哈
$input_array = $_GET['paramsName'];
参考链接php将数据存到数组中:
Q4: php如何将同一个字段多次提交的值存放在一个数组里
可以使用数组递归合并函数 array_merge()
array_merge() 函数把一个或多个数组合并为一个数组
?php
$a1=array("a"="red","b"="green");
$a2=array("c"="blue","d"="yellow");
$a3=array("e"="black");
print_r(array_merge($a1,$a2,$a3));
?
//以上代码会输出以下内容php将数据存到数组中:
Array
(
[a] = red
[b] = green
[c] = blue
[d] = yellow
[e] = black
)
Q5: php查询到多条数据结果后,将每一条结果放入数组
PHP查询到的数据存放到数组里面php将数据存到数组中,一般使用$arr[]=$row的方式实现php将数据存到数组中,$row是mysql_fetch_array获得的一行数据php将数据存到数组中,本身是一个数组,执行上面的语句之后,这一行会添加存放在额为数组$arr的最后。
典型的例子代码是这样的:
mysql_connect('127.0.0.1', 'root', '123456');
$sql='select * from test.tab';
if ($res=mysql_query($sql)){
while($row=mysql_fetch_array($res)) $result[]=$row;
mysql_free_resule($res);
}else echo "执行SQL语句:$sqlbr\n错误:".mysql_error();
echo '查询结果在下面的额为数组里面:pre';
print_r($result);
echo '/pre';
php将数据存到数组中的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php将数据存到数组中的方法、php将数据存到数组中的信息别忘了在本站进行查找喔。







