当前位置:Gxlcms > PHP教程 > 讲解php中pdo占位符的使用方法

讲解php中pdo占位符的使用方法

时间:2021-07-01 10:21:17 帮助过:13人阅读

pdo占位符在php使用有着重要的作用,本篇文章将介绍其使用方法。

$dbms='mysql';//数据库类型
$host='localhost';//数据库主机名
$dbName='test';//使用的数据库
$user='root';//数据库连接用户名
$pass='root';//对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
try{
$db=newPDO($dsn,$user,$pass);//初始化一个PDO对象
}catch(PDOException$e){
die("Error!:".$e->getMessage()."
");
}
$username=isset($_GET['username'])?$_GET['username']:'';
$ip=isset($_GET['ip'])?$_GET['ip']:'';
$mac=isset($_GET['mac'])?$_GET['mac']:'';
$apMac=isset($_GET['apMac'])?$_GET['apMac']:'';
$loginTime=isset($_GET['loginTime'])?$_GET['loginTime']:'';
$time=date('Y-m-dH:i:s');
//编写sql使用占位符
$sql="insertintotest(username,ip,mac,apMac,loginTime,insert_time)
values(:username,:ip,:mac,:apMac,:loginTime,'$time')";
$query=$db->prepare($sql);
/**
感觉挺麻烦的
$query->bindParam(':username',$username);
$query->bindParam(':ip',$ip);
$query->bindParam(':mac',$mac);
$query->bindParam(':apMac',$apMac);
$query->bindParam(':loginTime',$loginTime);
$query->execute();
**/
$result=$query->execute(array(
    ':username'=>$username,
    ':ip'=>$ip,
    ':mac'=>$mac,
    ':apMac'=>$apMac,
    ':loginTime'=>$loginTime,
));

本篇介绍了pdo占位符的使用方法,更多相关内容请关注Gxl网。

相关推荐:

列举PHP空值检测函数及方法

PHP连接数据库,通过面向过程方法实现最基本的增删改查操作

如何通过PHP实现Mysql数据库连接、查询、记录集等操作

以上就是讲解php中 pdo占位符的使用方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行