PHP基于PDO扩展操作mysql数据库示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-03 11:37:04 浏览: 评论:0
这篇文章主要介绍了PHP基于PDO扩展操作mysql数据库,结合实例形式分析了php使用pdo扩展针对mysql数据库的查询、插入、修改等操作实现技巧,需要的朋友可以参考下。
本文实例讲述了PHP基于PDO扩展操作mysql数据库。分享给大家供大家参考,具体如下:
SELECT
- //查单条
- function get_sub_product()
- {
- $res_zz = $pdo->query("SELECT id FROM content WHERE is_publish = 0 LIMIT 1;");
- $rep = $res_zz->fetch();
- }
- //查多条
- function get_sub_product()
- {
- $dsn = 'mysql:host=localhost;dbname=bwdb';
- $pdo = new PDO($dsn, 'root', 'root');
- $pdo->query('set names utf8');
- $res_zz = $pdo->query("SELECT product_id FROM sub_product WHERE 1= 1;");
- $rep = $res_zz->fetchAll();
- $product_id_arr = array();
- foreach ($rep as $v) {
- $product_id_arr[] = $v['product_id'];
- }
- return $product_id_arr;
- }
INSERT
- function add()
- {
- try {
- $pdo->beginTransaction();
- $reb = $pdo->prepare("INSERT INTO `content` (`id`,`content`) VALUES(:version_id,:content);");
- $reb->bindParam(':version_id', $version_id, PDO::PARAM_INT);
- $reb->bindParam(':content', $content, PDO::PARAM_STR);
- $type_id = 1;
- $reb->bindParam(':type_id', $type_id, PDO::PARAM_INT);
- $reb->bindParam(':cdate', $time, PDO::PARAM_STR);
- $reb->bindParam(':mdate', $time, PDO::PARAM_STR);
- if ($reb->execute() > 0) {
- $pdo->commit();
- die("成功!");
- }
- $pdo->rollback();
- die("失败!");
- } catch (PDOException $ex) {
- echo($ex->getMessage());
- }
- }
- function add($res_arr)
- {
- $dsn = 'mysql:host=localhost;dbname=db';
- $pdo = new PDO($dsn, 'root', 'root');
- $pdo->query('set names utf8');
- $value = '';
- date_default_timezone_set("Asia/Shanghai"); //时区
- $time = date('Y-m-d h:i:s');
- foreach ($res_arr as $k => $v) {
- $hash = md5($v['ProductID'] . "-" . $v['CategoryID'] . "-" . $v['CategoryName']);
- $value .= "('" . $v['ProductID'] . "','" . $v['CategoryID'] . "','" . $v['CategoryName'] . "','" . $v['ProductName'] . "','" . $v['Abbreviation'] . "','" . $v['SubscribeStatus'] . "','" . $v['ProductStatus'] . "','" .
- $v['BrandID'] . "','" . $v['BrandName'] . "','" . $v['TradeType'] . "','" . $v['ProductModel'] . "','" . $v['UPCCode'] . "','" . $v['Weight'] . "','" . $v['Summary'] . "','" .
- $v['LongDescription'] . "','" . $v['BuyNotice'] . "','" . $v['PackingList'] . "','" . $v['DefaultImage'] . "','" . $v['Keywords'] . "','" . $v['VendorID'] . "','" . $v['StoreID'] . "','" .
- $v['CurrentPrice'] . "','" . $v['AvailableQty'] . "','" . $hash . "','" . $time . "','" . $time . "'),";
- }
- $vv = substr($value, 0, strlen($value) - 1);
- try {
- $pdo->beginTransaction();
- if (!emptyempty($vv)) {
- $reb = $pdo->prepare("REPLACE INTO `product_list` (`product_id`, `category_id`, `category_name`, `product_name`, `abbreviation`, `subscribe_status`, `product_status`, `brand_id`, `brand_name`, `trade_type`, "
- . "`product_model`, `upc_code`, `weight`, `summary`, `long_description`, `buy_notice`, `packing_list`, `default_image`, `keywords`, `vendor_id`, `store_id`, `current_price`, "
- . "`available_qty`, `hash`, `cdate`, `mdate`) VALUES $vv;");
- if ($reb->execute() > 0) {
- $pdo->commit();
- return 1;
- }
- }
- } catch (PDOException $ex) {
- $pdo->rollback();
- }
- return -1;
- }
UPDATE
- function update()
- {
- try {
- $pdo->beginTransaction();
- $is_publish = 1;
- $req = $pdo->prepare("UPDATE `content` SET is_pub=:is_pub,mdate=:mdate WHERE id = :id;");
- $req->bindParam(':is_publish', $is_publish, PDO::PARAM_INT);
- $req->bindParam(':id', $version_id, PDO::PARAM_INT);
- $req->bindParam(':mdate', $time, PDO::PARAM_STR);
- if ($req->execute()) {
- $pdo->commit();
- die("成功!");
- }
- $pdo->rollback();
- die("失败!");
- } catch (PDOException $ex) {
- echo($ex->getMessage());
- }
- }
Tags: PDO扩展 PHP扩展
相关文章
- ·php学习笔记之PHP扩展开发教程(2014-08-27)
- ·php扩展开发实例详解y(2015-04-15)
- ·浅谈php扩展imagick(2021-01-31)
- ·PHP扩展CURL的用法详解(2021-02-28)
- ·PHP扩展模块memcached长连接使用方法分析(2021-05-04)
- ·PHP扩展程序实现守护进程(2021-05-22)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)