一:创建sql.php文件
<?php
function sqlMethod($sql)
{
$servername = "localhost";
$username = "root";
$password = "root";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 执行sql
if ($conn->query($sql) === TRUE) {
echo "成功";
} else {
echo "失败,失败信息: " . $conn->error;
}
$conn->close();
} 二:创建index.php
<?php
//引入sql.php文件
include 'sql.php';
//需要传递的参数(要执行的sql语句)
$sql = "CREATE DATABASE myDB";
//输出执行结果
echo sqlMethod($sql);