
if (! mysql_connect('127.0.0.1','root','123456')) exit('数据库链接失败!')
$sql='select * from ku.biao'
$res=mysql_query($sql)
if (! $res) exit("执行查询失败,SQL:$sql错误:".mysql_error())
echo '<table border=1><tr><th>会员ID<th>注册名称'
while($row=mysql_fetch_row($res)) echo "<tr><td>$row[0]<td>$row[1]"
echo '</table>'
mysql_free_result($res)
mysql_close()
?>
你执行后拷屏我看,不可能只列出一个,应该是列出所有。
一般我们为了减少数据库链接,取数据是一次取出所有想要的数据然后做循环处理,而不是一个个循环取出
$servername = "localhost"$username = "root"
$password = "root"
$dbname = "aaaa"
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname)
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error)
}
$conn->query('set names utf8')
$sql = "SELECT name FROM xiao "//这里是查询xiao表的name列的所有数据
$result = $conn->query($sql)
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
//print_r($row)
echo "name: " . $row["name"]."<br>"//这里是循环打印
}
} else {
echo "没有查询到数据"
}
$conn->close()
<?php$con = mysql_connect("localhost","root","")//连接数据库
mysql_select_db('test')//选择数据库
?>
<html>
<head>
<title>dropdown from mysql</title>
</head>
<body>
<h1>dropdown from mysql</h1>
<form action="#" method="post">
<select>
<option value=0>--请选择--</option>
<?php
$sql= "select val from custom where field='hook_load'"//sql语句
$result = mysql_query($sql, $con)//执行sql语句
while($row = mysql_fetch_array($result))
{
echo "<option value='$row[val]'>$row[val]</option>"//循环,拼凑下拉框选项
}
?>
</select>
</form>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)