Use of php pdo placeholder (explanation with code examples)

jacklove
Release: 2023-03-27 15:02:02
Original
2804 people have browsed it

This article explains the use of php pdo placeholder.

Use of php pdo placeholder (code example explanation)

Use of php pdo

Prevent sql injection

$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,
));
Copy after login

This article explains php pdo placeholder The use of bit symbols, please pay attention to the php Chinese website for more related content.

Related recommendations:

PHP Implementation of Bucket Sorting Algorithm

##Detailed Explanation of Merge Sorting of PHP Sorting Algorithm Series

thinkPHP5 framework database coherent operation: cache() usage details

The above is the detailed content of Use of php pdo placeholder (explanation with code examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!