php pdo 参数绑定
Posted mingzhanghui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php pdo 参数绑定相关的知识,希望对你有一定的参考价值。
* 数据表
-- mysql dump 10.16 Distrib 10.1.31-MariaDB, for osx10.6 (i386) -- -- Host: localhost Database: laravel -- ------------------------------------------------------ -- Server version 10.1.31-MariaDB /*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; /*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; /*!40101 SET @[email protected]@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @[email protected]@TIME_ZONE */; /*!40103 SET TIME_ZONE=‘+00:00‘ */; /*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @[email protected]@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO‘ */; /*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `users` -- DROP TABLE IF EXISTS `users`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `password` varchar(60) NOT NULL, `remember_token` varchar(100) DEFAULT NULL, `created_at` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00‘, `updated_at` timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00‘, PRIMARY KEY (`id`), UNIQUE KEY `users_email_unique` (`email`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `users` -- LOCK TABLES `users` WRITE; /*!40000 ALTER TABLE `users` DISABLE KEYS */; INSERT INTO `users` VALUES (1,‘mingzhanghui‘,‘[email protected]‘,‘123456‘,NULL,‘2018-06-30 16:00:00‘,‘2018-07-12 16:00:00‘),(2,‘username#0‘,‘[email protected]‘,‘123456‘,‘ed3d2c21991e3bef5e069713af9fa6ca‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(3,‘username#1‘,‘[email protected]‘,‘123456‘,‘3416a75f4cea9109507cacd8e2f2aefc‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(4,‘username#2‘,‘[email protected]‘,‘123456‘,‘43ec517d68b6edd3015b3edc9a11367b‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(5,‘username#3‘,‘[email protected]‘,‘123456‘,‘182be0c5cdcd5072bb1864cdee4d3d6e‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(6,‘username#4‘,‘[email protected]‘,‘123456‘,‘2a38a4a9316c49e5a833517c45d31070‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(7,‘username#5‘,‘[email protected]‘,‘123456‘,‘ac627ab1ccbdb62ec96e702f07f6425b‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(8,‘username#6‘,‘[email protected]‘,‘123456‘,‘2a38a4a9316c49e5a833517c45d31070‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(9,‘username#7‘,‘[email protected]‘,‘123456‘,‘f033ab37c30201f73f142449d037028d‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(10,‘username#8‘,‘[email protected]‘,‘123456‘,‘03afdbd66e7929b125f8597834fa83a4‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘),(11,‘username#9‘,‘[email protected]‘,‘123456‘,‘33e75ff09dd601bbe69f351039152189‘,‘2018-07-21 09:28:37‘,‘2018-07-21 09:29:37‘); /*!40000 ALTER TABLE `users` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET [email protected]_TIME_ZONE */; /*!40101 SET [email protected]_SQL_MODE */; /*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */; /*!40014 SET [email protected]_UNIQUE_CHECKS */; /*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; /*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; /*!40101 SET [email protected]_COLLATION_CONNECTION */; /*!40111 SET [email protected]_SQL_NOTES */; -- Dump completed on 2018-07-22 12:07:18
* php 代码片段
<?php $settings = [ ‘host‘ => ‘127.0.0.1‘, ‘port‘=> 3306, ‘name‘ => ‘laravel‘, ‘username‘ => ‘root‘, ‘password‘ => ‘‘, ‘charset‘ => ‘utf8‘ ]; try { $pdo = new PDO( sprintf( ‘mysql:host=%s;dbname=%s;port=%d;charset=%s‘, $settings[‘host‘], $settings[‘name‘], $settings[‘port‘], $settings[‘charset‘] ), $settings[‘username‘], $settings[‘password‘] ); } catch (PDOException $e) { echo "Database connection failed: ".$e->getMessage(); exit; } // $sql = ‘SELECT * from users where email=:email‘; $sql = ‘SELECT * from users where id < :id‘; $statement = $pdo->prepare($sql); // $email = filter_input(INPUT_GET, ‘email‘); // $statement->bindValue(‘:email‘, ‘[email protected]‘, PDO::PARAM_STR); $statement->bindValue(‘:id‘, 8, PDO::PARAM_INT); $statement->execute(); // while (($result = $statement->fetch(PDO::FETCH_ASSOC)) !== false) { // echo $result[‘email‘].PHP_EOL; // } while (($o = $statement->fetchObject()) !== false) { echo $o->email.PHP_EOL; } // transanction // $pdo->beginTransaction(); // ... // $pdo->commit();
* output
chenhuimingdeMacBook-Pro:database Mch$ php pdo.php
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
以上是关于php pdo 参数绑定的主要内容,如果未能解决你的问题,请参考以下文章
PHP PostgreSQL PDO 无法使用 LIKE 绑定参数