存储从MySQL表的值到PHP中的数组 [英] storing values from mysql table into an array in php
本文介绍了存储从MySQL表的值到PHP中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我GOOGLE了四周,发现没有有效的解决方案。希望这里有人会有所帮助。
这是我的code:
< PHP
包括../includes/connection.php
$ sql_select中=选择从标题的问题; 如果(!$结果= mysql_query($ sql_select中))
{
回声错误< BR>中。 mysql_error($ sql_select中);
死();
}
如果(mysql_num_rows($结果)== 0){
回声没有问题!
}
其他{ $标题= mysql_fetch_array($结果);
的print_r($标题);
}
?>
对于我的web应用程序的目的,我需要把问题标题进入一个新的数组。
我以为mysql_fetch_array()函数本身创建一个数组,但我想我错了。
任何帮助吗?
谢谢
解决方案
$结果= mysql_query('*从表中选择');$表=阵列();
而($ R = mysql_fetch_array($结果){
$行=阵列();
的foreach($ R $为K => $ V){
$行[$ K] = $ V;
}
array_push($表,$行);
未设置($行);
}
$表将是一个三维阵列重新您的表presentation。 $表[0] ['标题']将是它的标题是第一行。
I googled around and found no valid solutions. Hope someone here will help. This is my code:
<?php
include "../includes/connection.php";
$sql_select="SELECT title FROM questions";
if (!$result=mysql_query($sql_select))
{
echo "Error<br>" . mysql_error($sql_select);
die();
}
if (mysql_num_rows($result)==0) {
echo "No questions!";
}
else {
$titles = mysql_fetch_array($result);
print_r($titles);
}
?>
For the purpose of my web application I need to put question titles into a new array. I thought mysql_fetch_array() function creates an array by itself, but I guess I was wrong. Any help? Thanks
解决方案
$result = mysql_query('select * from table');
$table = array();
while($r = mysql_fetch_array($result) {
$row = array();
foreach($r as $k=>$v) {
$row[$k] = $v;
}
array_push($table,$row);
unset($row);
}
$table will be a 3d array representation of your table. $table[0]['title'] will be the title of it's first row.
这篇关于存储从MySQL表的值到PHP中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文