要扩展MySQL,需要在PHP中启用MySQL扩展。以下是启用MySQL扩展的示例代码:
// 连接MySQL数据库
$con = mysqli_connect("localhost", "username", "password", "database_name");
// 检查连接是否成功
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// 执行MySQL查询
$result = mysqli_query($con, "SELECT * FROM table_name");
// 处理查询结果
while($row = mysqli_fetch_array($result)) {
echo $row["column_name"];
}
// 关闭MySQL连接
mysqli_close($con);
在上面的示例中,mysqli_connect()
函数用于连接到MySQL数据库,mysqli_query()
函数用于执行查询,mysqli_fetch_array()
函数用于处理查询结果,mysqli_close()
函数用于关闭连接。这些函数都是MySQL扩展的一部分。