PHP操作Mysql

省略php的安装配置,以及如何配置对mysql的支持。

操作代码如下:

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "123456";
$mysql_database = "db";


$sql = "SELECT id,user_login,user_email FROM wp_users";


// 创建连接
$conn = new mysqli($servername, $username, $password, $mysql_database);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". $row["id"]. " - Name: ". $row["user_login"]. " " . $row["user_email"];
    }
} else {
    echo "0 个结果";
}
$conn->close();
?>