一般我们为了减少数据库链接,取数据是一次取出所有想要的数据然后做循环处理,而不是一个个循环取出
$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"]."
";//这里是循环打印
}
} else {
echo "没有查询到数据";
}
$conn->close();
header("Content-Type:text/html;charset=utf-8");
$pdo = new PDO("mysql:host=localhost;dbname=test","root","root");
$sql="select content from test where id = 25";
$res=$pdo -> query($sql);
$row = $res -> fetch();
if($res){
echo "查询成功!";
}
echo "
";
print_r($row);