试编写代码如下:
<?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()
?>
可以使用pdo链接数据库读取数据:
<?php//构造PDO连接
$dbh = "mysql:host=localhostdbname=test"
$db = new PDO($dbh, 'root', 'root123')
$db->query("set character set 'gbk'")
//查询数据
$sql = "SELECT * FROM info"
$sth = $db->query($sql)
while($row = $sth->fetch()){
echo "用户业务类型:".$row['leixing']."<br />"
echo "业务编号:".$row['bianhao']."<br />"
echo "注册序号:".$row['xuhao']."<br /><br />"
}
$db = null
?>
大致流程就是这样,
具体的输出样式需要自己定义
$urls = "192.168.0.128"//你的主机地址(一般是localhost)$user = "root"//数据库用户名
$password = "111111"//数据库密码
$con = mysqli_connect($urls,$user,$password)//mysql数据库连接
mysqli_select_db($con,"le_test")//第二个参数为数据库名称
mysqli_query($con,"select click_num from fstk_zhi_click where id>0")//查询所有click_num
mysqli_close($con)//关闭连接
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)