如何通过PDO实现高效且稳定的数据库连接与操作?

2026-04-02 13:361阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计279个文字,预计阅读时间需要2分钟。

如何通过PDO实现高效且稳定的数据库连接与操作?

在database.php文件中返回所需数组,不超过100个字:

phpreturn [ 'type'=> $type ? 'mysql' : 'mysql', 'port'=> $port ? $port : '3306', 'username'=> $username ? $username : 'root', 'password'=> $password ? $password : '', 'dbname'=> $dbname ? $dbname : 'chloe', 'host'=> 'localhost'];

在database.php返回需要的数组

在database.php返回需要的数组

  • return [
  • 'type' => $type ?? 'mysql',
  • 'port' => $port ?? '3306',
  • 'username' => $username ?? 'root',
  • 'password' => $password ?? '',
  • 'dbname' => $dbname ?? 'chloe',
  • 'host' => $host ?? 'localhost',
  • 'charset' => $charset ?? 'utf8',
  • ];
  • 数据库连接

  • //配置文件引过来
  • $config = require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
  • extract($config);
  • $dsn = sprintf('%s:host=%s;port=%s;dbname=%s', $type, $host, $port, $dbname);
  • try {
  • $pdo = new PDO($dsn, $username, $password);
  • var_dump($pdo);
  • } catch (PDOException $e) {
  • die($e->getMessage());
  • }
  • // 后端可接受前端传过来的参数
  • $name = isset($_POST['username']) ? $_POST['username'] : null;
  • $pwd = isset($_POST['password']) ? $_POST['password'] : null;
  • $pwd = md5($pwd);
  • // sql模板
  • $sql = "SELECT `username`,`password` FROM `user` WHERE `username` = ? AND `password` = ? ";
  • // prepare准备阶段
  • $stmt = $pdo->prepare($sql);
  • // 为占位符绑定参数
  • // $stmt->bindParam(1, $name);
  • // $stmt->bindParam(2, $pwd);
  • $res = $stmt->execute([$name, $pwd]);
  • if ($res) {
  • $res = $stmt->fetch(PDO::FETCH_ASSOC);
  • if ($res) {
  • echo json_encode(['status' => 1, 'msg' => '登录成功']);
  • }
  • }
  • 如何通过PDO实现高效且稳定的数据库连接与操作?

    本文共计279个文字,预计阅读时间需要2分钟。

    如何通过PDO实现高效且稳定的数据库连接与操作?

    在database.php文件中返回所需数组,不超过100个字:

    phpreturn [ 'type'=> $type ? 'mysql' : 'mysql', 'port'=> $port ? $port : '3306', 'username'=> $username ? $username : 'root', 'password'=> $password ? $password : '', 'dbname'=> $dbname ? $dbname : 'chloe', 'host'=> 'localhost'];

    在database.php返回需要的数组

    在database.php返回需要的数组

  • return [
  • 'type' => $type ?? 'mysql',
  • 'port' => $port ?? '3306',
  • 'username' => $username ?? 'root',
  • 'password' => $password ?? '',
  • 'dbname' => $dbname ?? 'chloe',
  • 'host' => $host ?? 'localhost',
  • 'charset' => $charset ?? 'utf8',
  • ];
  • 数据库连接

  • //配置文件引过来
  • $config = require_once __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
  • extract($config);
  • $dsn = sprintf('%s:host=%s;port=%s;dbname=%s', $type, $host, $port, $dbname);
  • try {
  • $pdo = new PDO($dsn, $username, $password);
  • var_dump($pdo);
  • } catch (PDOException $e) {
  • die($e->getMessage());
  • }
  • // 后端可接受前端传过来的参数
  • $name = isset($_POST['username']) ? $_POST['username'] : null;
  • $pwd = isset($_POST['password']) ? $_POST['password'] : null;
  • $pwd = md5($pwd);
  • // sql模板
  • $sql = "SELECT `username`,`password` FROM `user` WHERE `username` = ? AND `password` = ? ";
  • // prepare准备阶段
  • $stmt = $pdo->prepare($sql);
  • // 为占位符绑定参数
  • // $stmt->bindParam(1, $name);
  • // $stmt->bindParam(2, $pwd);
  • $res = $stmt->execute([$name, $pwd]);
  • if ($res) {
  • $res = $stmt->fetch(PDO::FETCH_ASSOC);
  • if ($res) {
  • echo json_encode(['status' => 1, 'msg' => '登录成功']);
  • }
  • }
  • 如何通过PDO实现高效且稳定的数据库连接与操作?