
正文
从php文件中取出数据,php导出大量数据
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
将PHP文件取出的数据库数据显示在前端HTML文件某个div中的几种方法
第一种,使用smarty模板引擎
php文件:
$smarty-assign('data','hello world');
$smarty-display('index.html');
index.html文件:
div{$data}/div
输出hello world
第二种,使用PHP变量直接输出
php文件:
$data = 'hello world';
require 'index.html';
index.html:文件:
div?php echo $data;?/div
相关问答
Q1: php中如何提取数据?
有很多方法的呀,
1)字符串截取,$result
=
substr($whole,
0,
4);
2)用空格分割字符串到数组中:$ary
=
explode('
',
$whole);
$result
=
$ary[0]
Q2: php如何取数据库中内容
试编写代码如下:
?php
//从数据库根据 id 获取颜色
function getColor($db, $id)
{
if ($result = $db-query("SELECT * FROM color where id='" . $id . "'"))
{
$row = $result-fetch_assoc();
return $row['color'];
}
return '#000000';
}
$mysqli = new mysqli("localhost", "test", "test", "room");
if ($mysqli-connect_error) {
printf("数据库连接错误: %s\n", mysqli_connect_error());
exit();
}
?
table border="1" cellspacing="0"
tr
td bgcolor="?php echo getColor($mysqli,'1')?"1/td
/tr
tr
td bgcolor="?php echo getColor($mysqli,'2')?"2/td
/tr
tr
td bgcolor="?php echo getColor($mysqli,'3')?"3/td
/tr
/table
?php
$mysqli-close();
?
Q3: php中怎么从数据库中取出数据?怎么显示?最好有详细的代码
$con=mysql_connect('localhost','root','');//数据库信息
mysql_select_db('shop');//数据库名
mysql_query("set names utf8");//设置字符集编码
$sql="select goods_name,goods_number,shop_price from goods";//查询语句
$res=mysql_query($sql);//执行查询
while($row=mysql_fetch_assoc($res)){
$rows[]=$row;//接受结果集
}
//遍历数组
foreach($rows as $key=$v){
echo $v['goods_name']."---".$v['goods_number']."---".$v['shop_price']."br/";
}






