<?php
return [
//数据库类型
'type' => 'mysql',
//数据库主机
'host' => 'localhost',
// 用户名
'User' => 'root',
// 密码
'password' => 'root',
// 端口号
'port' => '3306',
// 字符集
'charset' => 'utf8',
//数据库
'dbName' => 'MyUser',
];
<?php
$config = require __DIR__.'/01.php';
//print_r($config);
// 解析配置
['type' => $type,'charset' => $charset, 'host' =>$host , 'User' => $user, 'password' => $password , 'dbName' =>$shuJuKu , 'port' => $port] = $config;
//创建数据原
$tmp = '%s:host=%s;dbname=%s;port=%s;charset=%s'; //创建模板
$coon = [$type , $host , $shuJuKu , $port , $charset];
$dsn = vsprintf($tmp , $coon) ;
//连接数据库
try{
$db = new PDO($dsn, $user , $password);
if ($db) {
//echo '链接成功';
}
}catch (PDOException $e) {
die ('连接失败'.$e->getMessage());
}
<?php
require 'php/config/1202.php';
$pas = 101 ;
// 增
$sql = 'INSERT user values (null ,?,?,?)' ;
$stmt = $db->prepare($sql) ;
if ($stmt->execute(['1','小王',md5($pa)])){
echo 'cg'.$db->lastInsertId();
}else{
echo 'n------';
}
//改
$sql = 'UPDATE user SET uname = ? WHERE ID =?' ;
//判断是否有条件更新
if(!stristr($sql,'where')) {
die('禁止无条件更新');
}
$stmt = $db->prepare($sql) ;
if ($stmt->execute(['白小纯','*'])){
//判断影响行数
echo '受影响行数 = ' . $stmt->rowCount();
}else{
$stmt->debugDumpParams();
echo '更新失败';
}
// 删
$sql = 'DELETE FROM user WHERE ID = ?' ;
//判断是否有条件更新
if(!stristr($sql,'where')) {
die('禁止无条件删除');
}
$stmt = $db->prepare($sql) ;
if ($stmt->execute(['4'])){
//判断影响行数
echo '删除成功 受影响行数 = ' . $stmt->rowCount();
}else{
$stmt->debugDumpParams();
echo '更新失败';
}
//查
$sql = 'SELECT uname = ? FROM user ' ;
$stmt = $db->prepare($sql) ;
if ($stmt->execute(['白小纯'])){
//判断影响行数
echo '查询成功 查询到行数 = ' . $stmt->rowCount();
}else{
$stmt->debugDumpParams();
echo '查询失败';
}
while ($re[] = $stmt->fetch(PDO::FETCH_ASSOC));
print_r($re);