多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 连接 MySQL 在我们访问 MySQL 数据库前,我们需要先连接到数据库服务器: ### MySQLi - 面向对象 ```php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = new mysqli($servername, $username, $password); // 检测连接 if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ``` ### MySQLi - 面向过程 ```php $servername = "localhost"; $username = "username"; $password = "password"; // 创建连接 $conn = mysqli_connect($servername, $username, $password); // 检测连接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ``` ### OOP ```php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); echo "Connected successfully"; } catch(PDOException $e){ echo $e->getMessage(); } ```