以下是使用PHP连接MySQL数据库的详细代码示例,我们将使用PDO(PHP Data Objects)来连接和操作数据库,因为它提供了更高层次的抽象和更好的安全性。
安装PDO扩展
确保你的PHP环境已经安装了PDO和PDO_MYSQL扩展,如果没有,请根据你的操作系统进行安装。
创建数据库和表
我们需要一个MySQL数据库和一个表,假设我们有一个名为testdb
的数据库,其中有一个名为users
的表。
CREATE DATABASE testdb; USE testdb; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL );
PHP代码示例
下面是一个完整的PHP脚本,用于连接到MySQL数据库并执行基本的CRUD操作。
3.1 配置数据库连接参数
<?php $host = 'localhost'; // 数据库主机名 $dbname = 'testdb'; // 数据库名称 $username = 'root'; // 数据库用户名 $password = ''; // 数据库密码 ?>
3.2 连接到数据库
<?php try { $dsn = "mysql:host=$host;dbname=$dbname"; $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
3.3 插入数据
<?php try { $sql = "INSERT INTO users (username, email) VALUES (:username, :email)"; $stmt = $pdo->prepare($sql); $stmt->execute(['username' => 'john_doe', 'email' => 'john@example.com']); echo "New record created successfully"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } ?>
3.4 查询数据
<?php try { $sql = "SELECT id, username, email FROM users"; $stmt = $pdo->query($sql); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo "ID: " . $row['id'] . " Name: " . $row['username'] . " Email: " . $row['email'] . "<br>"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } ?>
3.5 更新数据
<?php try { $sql = "UPDATE users SET email=:email WHERE id=:id"; $stmt = $pdo->prepare($sql); $stmt->execute(['email' => 'new_email@example.com', 'id' => 1]); echo "Record updated successfully"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } ?>
3.6 删除数据
<?php try { $sql = "DELETE FROM users WHERE id=:id"; $stmt = $pdo->prepare($sql); $stmt->execute(['id' => 1]); echo "Record deleted successfully"; } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } ?>
完整代码示例
将上述代码片段组合在一起,形成一个完整的PHP脚本:
<?php $host = 'localhost'; // 数据库主机名 $dbname = 'testdb'; // 数据库名称 $username = 'root'; // 数据库用户名 $password = ''; // 数据库密码 try { $dsn = "mysql:host=$host;dbname=$dbname"; $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully<br>"; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage() . "<br>"; } // 插入数据 try { $sql = "INSERT INTO users (username, email) VALUES (:username, :email)"; $stmt = $pdo->prepare($sql); $stmt->execute(['username' => 'john_doe', 'email' => 'john@example.com']); echo "New record created successfully<br>"; } catch (PDOException $e) { echo "Error: " . $e->getMessage() . "<br>"; } // 查询数据 try { $sql = "SELECT id, username, email FROM users"; $stmt = $pdo->query($sql); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo "ID: " . $row['id'] . " Name: " . $row['username'] . " Email: " . $row['email'] . "<br>"; } } catch (PDOException $e) { echo "Error: " . $e->getMessage() . "<br>"; } // 更新数据 try { $sql = "UPDATE users SET email=:email WHERE id=:id"; $stmt = $pdo->prepare($sql); $stmt->execute(['email' => 'new_email@example.com', 'id' => 1]); echo "Record updated successfully<br>"; } catch (PDOException $e) { echo "Error: " . $e->getMessage() . "<br>"; } // 删除数据 try { $sql = "DELETE FROM users WHERE id=:id"; $stmt = $pdo->prepare($sql); $stmt->execute(['id' => 1]); echo "Record deleted successfully<br>"; } catch (PDOException $e) { echo "Error: " . $e->getMessage() . "<br>"; } ?>
这个完整的PHP脚本展示了如何连接到MySQL数据库并进行基本的CRUD操作,你可以根据需要修改和扩展这些代码。
各位小伙伴们,我刚刚为大家分享了有关php连接mysql数据库代码_PHP代码样例的知识,希望对你们有所帮助。如果您还有其他相关问题需要解决,欢迎随时提出哦!
本文来源于互联网,如若侵权,请联系管理员删除,本文链接:https://www.9969.net/85039.html