大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关怎么在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 (!empty($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()); } }
上述就是小编为大家分享的怎么在PHP中使用PDO扩展操作mysql数据库了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。