欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP基于PDO扩展操作mysql数据库示例

程序员文章站 2022-04-10 09:57:30
本文实例讲述了php基于pdo扩展操作mysql数据库。分享给大家供大家参考,具体如下: select //查单条 function get_sub_prod...

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《php基于pdo操作数据库技巧总结》、《php+oracle数据库程序设计技巧总结》、《php+mongodb数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。