本文实例讲述了php实现通用的从数据库表读取数据到数组的函数。分享给大家供大家参考。具体分析如下:
按需制作网站可以根据自己的需求进行定制,成都网站设计、成都网站制作构思过程中功能建设理应排到主要部位公司成都网站设计、成都网站制作的运用实际效果公司网站制作网站建立与制做的实际意义
此函数不关心表结构,只需要指定表名、结构和查询条件既可以对表进行通用查询操作,非常实用。
function listmytablerows($table, $name, $field, $where, $textID) { / / Connect to the database and query execution connect (); $Sql = "select * from". $table. "". $where. "ORDER BY". $field; $Req = mysql_query($sql); $Res = mysql_num_rows($req); ? Select name = "?php echo $name; ?" id="?php echo $textID; ?" option value="" ____/ option ? Php / / We do a loop that will read the information for each record while ($data = mysql_fetch_array($res)) { / / We display the information from the current record ? Option value = "?php echo $data['id']; ?" ?php echo $data[$field]; ? / Option ? Php } ? / Select ? Php } ?
假设数据库结构如下
问卷表 wid
问题表 qid wid(属于哪个问卷) question
答案表 aid qid(属于哪个问题) answer
$sql='SELECT `wid` FROM `问卷表`';
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)){
$sql='SELECT `qid` FROM `问题表` WHERE wid='".$row['wid']."''; //选出该问卷的所有问题
$res2 = mysql_query($sql);
while($row2 = mysql_fetch_array($res2)){
$sql='SELECT `answer` FROM `答案表` WHERE qid='".$row2['qid']."''; //选出该问题的所有答案
$res3 = mysql_query($sql);
while($row3 = mysql_fetch_array($res3)){
switch($row3['answer']){
case 'A':
$answerA.$row2['qid'] = $answerA.$row2['qid'] + 1;
break;
case 'B':
$answerB.$row2['qid'] = $answerA.$row2['qid'] + 1;
break;
case 'C':
$answerC.$row2['qid'] = $answerA.$row2['qid'] + 1;
break;
case 'D':
$answerD.$row2['qid'] = $answerA.$row2['qid'] + 1;
break;
}
}
echo '问题'.$row2['qid'] .'统计:br/';
echo 'A'.$answerA.$row2['qid'].' B'.$answerB.$row2['qid'].' C'.$answerC.$row2['qid'].' D'.$answerD.$row2['qid'].'br/';
}
}
}
想用sql一次性查出来,是很困难的,在下才疏学浅,只能用本办法了
$con = mysql_connect('localhost', 'root', 'root');
mysql_select_db("php", $con);
$result = mysql_query("SELECT * FROM `game`");
$data_name=array();//有修改
while($rs = mysql_fetch_assoc($result)){
$data_name[] = $rs['name'];
}
print_r($data_name);//是你要的
假如 数据为 $value
$temp=explode(",",$value);
foreach($temp as $v){
$result[$v]=1;
}
然后html的时候可以这样:
if($result['admin']){
echo 'input type=checkbox /';
}
同理其他类型,可以foreach 输出
require_once('inc/conn.php');
mysql_query("set names GB2312");
$exec="select * from house_img where 2 ORDER BY Id DESC LIMIT 0, 10";
$result=mysql_query($exec,$conn);
$img=array();
while ($row=mysql_fetch_array($result)) $imd[]=$row['图片文件的字段名称'];
mysql_free_result($result);
数据库提到的数据一般是资源类型的,要逐一读出,添加到数组
while($row = mysql_fetch_assoc($res)){
$data[] = $row;
}