Home  >  Article  >  Backend Development  >  Explain how to use pdo placeholder in php

Explain how to use pdo placeholder in php

jacklove
jackloveOriginal
2018-06-08 10:50:593392browse

The pdo placeholder plays an important role in the use of PHP. This article will introduce how to use it.

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

This article introduces how to use pdo placeholders. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

List PHP null value detection functions and methods

PHP connects to the database through process-oriented methods The most basic add, delete, modify and query operations

How to implement Mysql database connection, query, record set and other operations through PHP

The above is the detailed content of Explain how to use pdo placeholder in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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