PHP实现的通过参数生成MYSQL语句类完整实例

本文实例讲述了PHP实现的通过参数生成MYSQL语句类。分享给大家供大家参考,具体如下:

这个类可以通过指定的表和字段参数创建SELECT ,INSERT , UPDATE 和 DELETE 语句。

这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFT JOIN和ORDER 语句

<?php
 /* *******************************************************************
Example file
This example shows how to use the MyLibSQLGen class
The example is based on the following MySQL table:
CREATE TABLE customer (
 id int(10) unsigned NOT NULL auto_increment,
 name varchar(60) NOT NULL default '',
 address varchar(60) NOT NULL default '',
 city varchar(60) NOT NULL default '',
 PRIMARY KEY (cust_id)
) TYPE=MyISAM;
******************************************************************* */
 require_once ( " class_mylib_SQLGen-1.0.php " );
 $fields = Array ( " name " , " address " , " city " );
 $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " );
 $tables = Array ( " customer " );
 echo  " <b>Result Generate Insert</b><br> " ;
 $object = new MyLibSQLGen();
 $object -> clear_all_assign(); // to refresh all property but it no need when first time execute
 $object -> setFields( $fields );
 $object -> setValues( $values );
 $object -> setTables( $tables );
 if ( ! $object -> getInsertSQL()){ echo  $object -> Error; exit ;}
 else { $sql = $object -> Result; echo  $sql . " <br> " ;}
 echo  " <b>Result Generate Update</b><br> " ;
 $fields = Array ( " name " , " address " , " city " );
 $values = Array ( " Fadjar " , " Resultmang Raya Street " , " Jakarta " );
 $tables = Array ( " customer " );
 $id = 1 ;
 $conditions [ 0 ][ " condition " ] = " id='$id' " ;
 $conditions [ 0 ][ " connection " ] = "" ;
 $object -> clear_all_assign();
 $object -> setFields( $fields );
 $object -> setValues( $values );
 $object -> setTables( $tables );
 $object -> setConditions( $conditions );
 if ( ! $object -> getUpdateSQL()){ echo  $object -> Error; exit ;}
 else { $sql = $object -> Result; echo  $sql . " <br> " ;}
 echo  " <b>Result Generate Delete</b><br> " ;
 $tables = Array ( " customer " );
 $conditions [ 0 ][ " condition " ] = " id='1' " ;
 $conditions [ 0 ][ " connection " ] = " OR " ;
 $conditions [ 1 ][ " condition " ] = " id='2' " ;
 $conditions [ 1 ][ " connection " ] = " OR " ;
 $conditions [ 2 ][ " condition " ] = " id='4' " ;
 $conditions [ 2 ][ " connection " ] = "" ;
 $object -> clear_all_assign();
 $object -> setTables( $tables );
 $object -> setConditions( $conditions );
 if ( ! $object -> getDeleteSQL()){ echo  $object -> Error; exit ;}
 else { $sql = $object -> Result; echo  $sql . " <br> " ;}
 echo  " <b>Result Generate List</b><br> " ;
 $fields = Array ( " id " , " name " , " address " , " city " );
 $tables = Array ( " customer " );
 $id = 1 ;
 $conditions [ 0 ][ " condition " ] = " id='$id' " ;
 $conditions [ 0 ][ " connection " ] = "" ;
 $object -> clear_all_assign();
 $object -> setFields( $fields );
 $object -> setTables( $tables );
 $object -> setConditions( $conditions );
 if ( ! $object -> getQuerySQL()){ echo  $object -> Error; exit ;}
 else { $sql = $object -> Result; echo  $sql . " <br> " ;}
 echo  " <b>Result Generate List with search on all fields</b><br> " ;
 $fields = Array ( " id " , " name " , " address " , " city " );
 $tables = Array ( " customer " );
 $id = 1 ;
 $search = " Fadjar Nurswanto " ;
 $object -> clear_all_assign();
 $object -> setFields( $fields );
 $object -> setTables( $tables );
 $object -> setSearch( $search );
 if ( ! $object -> getQuerySQL()){ echo  $object -> Error; exit ;}
 else { $sql = $object -> Result; echo  $sql . " <br> " ;}
 echo  " <b>Result Generate List with search on some fields</b><br> " ;
 $fields = Array ( " id " , " name " , " address " , " city " );
 $tables = Array ( " customer " );
 $id = 1 ;
 $search = Array (
       " name " => " Fadjar Nurswanto " ,
       " address " => " Tomang Raya "
    );
 $object -> clear_all_assign();
 $object -> setFields( $fields );
 $object -> setTables( $tables );
 $object -> setSearch( $search );
 if ( ! $object -> getQuerySQL()){ echo  $object -> Error; exit ;}
 else { $sql = $object -> Result; echo  $sql . " <br> " ;}
?>

类代码:

<?php
 /*
Created By    : Fadjar Nurswanto <fajr_n@rindudendam.net>
DATE      : 2006-08-02
PRODUCTNAME    : class MyLibSQLGen
PRODUCTVERSION  : 1.0.0
DESCRIPTION    : class yang berfungsi untuk menggenerate SQL
DENPENCIES    :
 */
 class MyLibSQLGen
{
   var  $Result ;
   var  $Tables = Array ();
   var  $Values = Array ();
   var  $Fields = Array ();
   var  $Conditions = Array ();
   var  $Condition ;
   var  $LeftJoin = Array ();
   var  $Search ;
   var  $Sort = " ASC " ;
   var  $Order ;
   var  $Error ;
   function MyLibSQLGen(){}
   function BuildCondition()
  {
     $funct = " BuildCondition " ;
     $className = get_class ( $this );
     $conditions = $this -> getConditions();
     if ( ! $conditions ){ $this -> dbgDone( $funct ); return  true ;}
     if ( ! is_array ( $conditions ))
    {
       $this -> Error = " $className::$funct Variable conditions not Array " ;
       return ;
    }
     for ( $i = 0 ; $i < count ( $conditions ); $i ++ )
    {
       $this -> Condition .= $conditions [ $i ][ " condition " ] . "  " . $conditions [ $i ][ " connection " ] . "  " ;
    }
     return  true ;
  }
   function BuildLeftJoin()
  {
     $funct = " BuildLeftJoin " ;
     $className = get_class ( $this );
     if ( ! $this -> getLeftJoin()){ $this -> Error = " $className::$funct Property LeftJoin was empty " ; return ;}
     $LeftJoinVars = $this -> getLeftJoin();
     $hasil = false ;
     foreach ( $LeftJoinVars  as  $LeftJoinVar )
    {
      @ $hasil .= " LEFT JOIN " . $LeftJoinVar [ " table " ];
       foreach ( $LeftJoinVar [ " on " ] as  $var )
      {
        @ $condvar .= $var [ " condition " ] . "  " . $var [ " connection " ] . "  " ;
      }
       $hasil .= " ON ( " . $condvar . " ) " ;
       unset ( $condvar );
    }
     $this -> ResultLeftJoin = $hasil ;
     return  true ;
  }
   function BuildOrder()
  {
     $funct = " BuildOrder " ;
     $className = get_class ( $this );
     if ( ! $this -> getOrder()){ $this -> Error = " $className::$funct Property Order was empty " ; return ;}
     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}
     $Fields = $this -> getFields();
     $Orders = $this -> getOrder();
     if ( ereg ( " , " , $Orders )){ $Orders = explode ( " , " , $Order );}
     if ( ! is_array ( $Orders )){ $Orders = Array ( $Orders );}
     foreach ( $Orders  as  $Order )
    {
       if ( ! is_numeric ( $Order )){ $this -> Error = " $className::$funct Property Order not Numeric " ; return ;}
       if ( $Order  >  count ( $this -> Fields)){ $this -> Error = " $className::$funct Max value of property Sort is " . count ( $this -> Fields); return ;}
      @ $xorder .= $Fields [ $Order ] . " , " ;
    }
     $this -> ResultOrder = " ORDER BY " . substr ( $xorder , 0 ,- 1 );
     return  true ;
  }
   function BuildSearch()
  {
     $funct = " BuildSearch " ;
     $className = get_class ( $this );
     if ( ! $this -> getSearch()){ $this -> Error = " $className::$funct Property Search was empty " ; return ;}
     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}
     $Fields = $this -> getFields();
     $xvalue = $this -> getSearch();
     if ( is_array ( $xvalue ))
    {
       foreach ( $Fields  as  $field )
      {
         if (@ $xvalue [ $field ])
        {
           $Values = explode ( "  " , $xvalue [ $field ]);
           foreach ( $Values  as  $Value )
          {
            @ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;
          }
           if ( $hasil )
          {
            @ $hasil_final .= " ( " . substr ( $hasil , 0 ,- 4 ) . " ) AND " ;
             unset ( $hasil );
          }
        }
      }
       $hasil = $hasil_final ;
    }
     else
    {
       foreach ( $Fields  as  $field )
      {
         $Values = explode ( "  " , $xvalue );
         foreach ( $Values  as  $Value )
        {
          @ $hasil .= $field . " LIKE '% " . $Value . " %' OR " ;
        }
      }
    }
     $this -> ResultSearch = substr ( $hasil , 0 ,- 4 );
     return  true ;
  }
   function clear_all_assign()
  {
     $this -> Result = null ;
     $this -> ResultSearch = null ;
     $this -> ResultLeftJoin = null ;
     $this -> Result = null ;
     $this -> Tables = Array ();
     $this -> Values = Array ();
     $this -> Fields = Array ();
     $this -> Conditions = Array ();
     $this -> Condition = null ;
     $this -> LeftJoin = Array ();
     $this -> Sort = " ASC " ;
     $this -> Order = null ;
     $this -> Search = null ;
     $this -> fieldSQL = null ;
     $this -> valueSQL = null ;
     $this -> partSQL = null ;
     $this -> Error = null ;
     return  true ;
  }
   function CombineFieldValue( $manual = false )
  {
     $funct = " CombineFieldsPostVar " ;
     $className = get_class ( $this );
     $fields = $this -> getFields();
     $values = $this -> getValues();
     if ( ! is_array ( $fields ))
    {
       $this -> Error = " $className::$funct Variable fields not Array " ;
       return ;
    }
     if ( ! is_array ( $values ))
    {
       $this -> Error = " $className::$funct Variable values not Array " ;
       return ;
    }
     if ( count ( $fields ) != count ( $values ))
    {
       $this -> Error = " $className::$funct Count of fields and values not match " ;
       return ;
    }
     for ( $i = 0 ; $i < count ( $fields ); $i ++ )
    {
      @ $this -> fieldSQL .= $fields [ $i ] . " , " ;
       if ( $fields [ $i ] ==  " pwd "  ||  $fields [ $i ] ==  " password "  ||  $fields [ $i ] ==  " pwd " )
      {
        @ $this -> valueSQL .= " password(' " . $values [ $i ] . " '), " ;
        @ $this -> partSQL .= $fields [ $i ] . " =password(' " . $values [ $i ] . " '), " ;
      }
       else
      {
         if ( is_numeric ( $values [ $i ]))
        {
          @ $this -> valueSQL .= $values [ $i ] . " , " ;
          @ $this -> partSQL .= $fields [ $i ] . " = " . $values [ $i ] . " , " ;
        }
         else
        {
          @ $this -> valueSQL .= " ' " . $values [ $i ] . " ', " ;
          @ $this -> partSQL .= $fields [ $i ] . " =' " . $values [ $i ] . " ', " ;
        }
      }
    }
     $this -> fieldSQL = substr ( $this -> fieldSQL , 0 ,- 1 );
     $this -> valueSQL = substr ( $this -> valueSQL , 0 ,- 1 );
     $this -> partSQL = substr ( $this -> partSQL , 0 ,- 1 );
     return  true ;
  }
   function getDeleteSQL()
  {
     $funct = " getDeleteSQL " ;
     $className = get_class ( $this );
     $Tables = $this -> getTables();
     if ( ! $Tables  ||  ! count ( $Tables ))
    {
       $this -> dbgFailed( $funct );
       $this -> Error = " $className::$funct Table was empty " ;
       return ;
    }
     for ( $i = 0 ; $i < count ( $Tables ); $i ++ )
    {
      @ $Table .= $Tables [ $i ] . " , " ;
    }
     $Table = substr ( $Table , 0 ,- 1 );
     $sql = " DELETE FROM " . $Table ;
     if ( $this -> getConditions())
    {
       if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
       $sql .= " WHERE " . $this -> getCondition();
    }
     $this -> Result = $sql ;
     return  true ;
  }
   function getInsertSQL()
  {
     $funct = " getInsertSQL " ;
     $className = get_class ( $this );
     if ( ! $this -> getValues()){ $this -> Error = " $className::$funct Property Values was empty " ; return ;}
     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}
     if ( ! $this -> getTables()){ $this -> Error = " $className::$funct Property Tables was empty " ; return ;}
     if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}
     $Tables = $this -> getTables();
     $sql = " INSERT INTO " . $Tables [ 0 ] . " ( " . $this -> fieldSQL . " ) VALUES ( " . $this -> valueSQL . " ) " ;
     $this -> Result = $sql ;
     return  true ;
  }
   function getUpdateSQL()
  {
     $funct = " getUpdateSQL " ;
     $className = get_class ( $this );
     if ( ! $this -> getValues()){ $this -> Error = " $className::$funct Property Values was empty " ; return ;}
     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}
     if ( ! $this -> getTables()){ $this -> Error = " $className::$funct Property Tables was empty " ; return ;}
     if ( ! $this -> CombineFieldValue()){ $this -> dbgFailed( $funct ); return ;}
     if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
     $Tables = $this -> getTables();
     $sql = " UPDATE " . $Tables [ 0 ] . " SET " . $this -> partSQL . " WHERE " . $this -> getCondition();
     $this -> Result = $sql ;
     return  true ;
  }
   function getQuerySQL()
  {
     $funct = " getQuerySQL " ;
     $className = get_class ( $this );
     if ( ! $this -> getFields()){ $this -> Error = " $className::$funct Property Fields was empty " ; return ;}
     if ( ! $this -> getTables()){ $this -> Error = " $className::$funct Property Tables was empty " ; return ;}
     $Fields = $this -> getFields();
     $Tables = $this -> getTables();
     foreach ( $Fields  as  $Field ){@ $sql_raw .= $Field . " , " ;}
     foreach ( $Tables  as  $Table ){@ $sql_table .= $Table . " , " ;}
     $this -> Result = " SELECT " . substr ( $sql_raw , 0 ,- 1 ) . " FROM " . substr ( $sql_table , 0 ,- 1 );
     if ( $this -> getLeftJoin())
    {
       if ( ! $this -> BuildLeftJoins()){ $this -> dbgFailed( $funct ); return ;}
       $this -> Result .= "  " . $this -> ResultLeftJoin;
    }
     if ( $this -> getConditions())
    {
       if ( ! $this -> BuildCondition()){ $this -> dbgFailed( $funct ); return ;}
       $this -> Result .= " WHERE ( " . $this -> Condition . " ) " ;
    }
     if ( $this -> getSearch())
    {
       if ( ! $this -> BuildSearch()){ $this -> dbgFailed( $funct ); return ;}
       if ( $this -> ResultSearch)
      {
         if ( eregi ( " WHERE " , $this -> Result)){ $this -> Result .= " AND " . $this -> ResultSearch;}
         else { $this -> Result .= " WHERE " . $this -> ResultSearch;}
      }
    }
     if ( $this -> getOrder())
    {
       if ( ! $this -> BuildOrder()){ $this -> dbgFailed( $funct ); return ;}
       $this -> Result .= "  " . $this -> ResultOrder;
    }
     if ( $this -> getSort())
    {
       if (@ $this -> ResultOrder)
      {
         $this -> Result .= "  " . $this -> getSort();
      }
    }
     return  true ;
  }
   function getCondition(){ return @ $this -> Condition;}
   function getConditions(){ if ( count (@ $this -> Conditions) &&  is_array (@ $this -> Conditions)){ return @ $this -> Conditions;}}
   function getFields(){ if ( count (@ $this -> Fields) &&  is_array (@ $this -> Fields)){ return @ $this -> Fields;}}
   function getLeftJoin(){ if ( count (@ $this -> LeftJoin) &&  is_array (@ $this -> LeftJoin)){ return @ $this -> LeftJoin;}}
   function getOrder(){ return @ $this -> Order;}
   function getSearch(){ return @ $this -> Search;}
   function getSort(){ return @ $this -> Sort ;}
   function getTables(){ if ( count (@ $this -> Tables) &&  is_array (@ $this -> Tables)){ return @ $this -> Tables;}}
   function getValues(){ if ( count (@ $this -> Values) &&  is_array (@ $this -> Values)){ return @ $this -> Values;}}
   function setCondition( $input ){ $this -> Condition = $input ;}
   function setConditions( $input )
  {
     if ( is_array ( $input )){ $this -> Conditions = $input ;}
     else { $this -> Error = get_class ( $this ) . " ::setConditions Parameter input not array " ; return ;}
  }
   function setFields( $input )
  {
     if ( is_array ( $input )){ $this -> Fields = $input ;}
     else { $this -> Error = get_class ( $this ) . " ::setFields Parameter input not array " ; return ;}
  }
   function setLeftJoin( $input )
  {
     if ( is_array ( $input )){ $this -> LeftJoin = $input ;}
     else { $this -> Error = get_class ( $this ) . " ::setFields Parameter input not array " ; return ;}
  }
   function setOrder( $input ){ $this -> Order = $input ;}
   function setSearch( $input ){ $this -> Search = $input ;}
   function setSort( $input ){ $this -> Sort = $input ;}
   function setTables( $input )
  {
     if ( is_array ( $input )){ $this -> Tables = $input ;}
     else { $this -> Error = get_class ( $this ) . " ::setTables Parameter input not array " ; return ;}
  }
   function setValues( $input )
  {
     if ( is_array ( $input )){ $this -> Values = $input ;}
     else { $this -> Error = get_class ( $this ) . " ::setValues Parameter input not array " ; return ;}
  }
}
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

(0)

相关推荐

  • php实现Mysql简易操作类

    自己封装的Mysql简易操作类,已塞在Ben框架中,基于PDO来写的,代码风格上有些无厘头... mysql.class.php <?php class mysql extends PDO{ public $server; public $database; public $user; public $password; public $sql; public function __construct($server,$database,$user,$password,$port=3306){

  • PHP使用Mysqli类库实现完美分页效果的方法

    本文实例讲述了PHP使用Mysqli类库实现完美分页效果的方法.分享给大家供大家参考,具体如下: 本篇文章是基于的是我的上篇文章<PHP数据库操作之基于Mysqli的数据库操作类库>而量身打造,怎么使用 M 类库中的 FetchAll 方法做出完美分页. 分页在我们每个项目中都是必不可少的,而且出现的频率非常之多.这样就要求我们程序员在项目中怎样去以最快的速度.最简洁的代码去实现分页方案. 分页的实现大部分是依据 URL 传入的参数(一般是page)来实现,比如:http://localhos

  • 十二个常见的PHP+MySql类免费CMS系统

    1. DEDECMS 一款国内开源的cms,作者是一个个人,能做出如此功能的cms,是相当不错的.2007版功能十分强大,希望能改善之前数据量一大,更新静态页就很慢的缺点.因为开源,有较多的玩家和拥护者.非常适合有一定编程基础的站长. 官方网站: dedecms.com 2. phpcms 一个综合的网站管理系统,由PHP+MYSQL构架全站生成html,能够快速高效地应用于LINUX和WINDOWS服务器平台,是目前中国LINUX环境下最佳的网站管理应用解决方案之一.据传被酷6收购. 官方网站

  • php简单操作mysql数据库的类

    本文实例讲述了php简单操作mysql数据库的类.分享给大家供大家参考.具体如下: <?php /** * Database class * * @version: 2.2 * @revised: 27 may 2007 * **/ class Database { var $host; var $name; var $user; var $pass; var $prefix; var $linkId; function Database($mysql) { foreach($mysql as

  • php封装的连接Mysql类及用法分析

    本文实例讲述了php封装的连接Mysql类及用法.分享给大家供大家参考,具体如下: class mysql{ private $db_name; private $db_host; private $db_user; private $db_pwd; private $conn; private $querysql; private $result; private $resultarray=array(); private $row; //创建构造函数 数据库名 主机名 用户名 密码 func

  • PHP实现基于mysqli的Model基类完整实例

    本文实例讲述了PHP实现基于mysqli的Model基类.分享给大家供大家参考,具体如下: DB.class.php <?php //数据库连接类 class DB { //获取对象句柄 static public function getDB() { $_mysqli = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME); if (mysqli_connect_errno()) { echo '数据库连接错误!错误代码:'.mysqli_connect_e

  • PHP格式化MYSQL返回float类型的方法

    本文实例讲述了PHP格式化MYSQL返回float类型的方法.分享给大家供大家参考,具体如下: PHP 中获取mysql的float字段,echo 输出后,小数部分为包含多个0. 可使用 floatval($num) 将0舍去. 如要保留小数位,可使用 number_format($num, 2); number_format函数对超过指定位数的值,进行了四舍五入. 如不想四舍五入,而保留所有小数.可使用如下方法: // 如仅想保留两位小数可用 number_format($num, 2); e

  • 一个php Mysql类 可以参考学习熟悉下

    复制代码 代码如下: <?php class Mysql { private $conn; private $host; private $username; private $password; private $dbname; private $pconnect; private $charset; public function __construct(array $params = null) { if (!empty($params)) { foreach ($params as $k

  • PHP基于单例模式实现的mysql类

    本文实例讲述了PHP基于单例模式实现的mysql类.分享给大家供大家参考,具体如下: <?php defined('ACC')||exit('Access Denied'); // 封装mysql操作类,包括连接功能,及查询功能. class mysql extends absdb{ protected static $ins = null; protected $host; // 主机名 protected $user; // 用户名 protected $passwd; // 密码 prot

  • PHP实现的通过参数生成MYSQL语句类完整实例

    本文实例讲述了PHP实现的通过参数生成MYSQL语句类.分享给大家供大家参考,具体如下: 这个类可以通过指定的表和字段参数创建SELECT ,INSERT , UPDATE 和 DELETE 语句. 这个类可以创建SQL语句的WHERE条件,像LIKE的查询语句,使用LEFT JOIN和ORDER 语句 <?php /* ******************************************************************* Example file This ex

  • python测试mysql写入性能完整实例

    本文主要研究的是python测试mysql写入性能,分享了一则完整代码,具体介绍如下. 测试环境: (1) 阿里云服务器centos 6.5 (2) 2G内存 (3) 普通硬盘 (4) mysql 5.1.73 数据库存储引擎为 InnoDB (5) python 2.7 (6) 客户端模块 mysql.connector 测试方法: (1) 普通写入 (2) 批量写入 (3) 事务加批量写入 普通写入: def ordinary_insert(count): sql = "insert int

  • Mybatis逆向生成使用扩展类的实例代码详解

    1.背景介绍 用的mybatis自动生成的插件,然而每次更改数据库的时候重新生成需要替换原有的mapper.xml文件,都要把之前业务相关的sql重新写一遍,感觉十分麻烦,就想着把自动生成的作为一个基础文件,然后业务相关的写在扩展文件里面,这样更改数据库后只需要把所有基础文件替换掉就可以了 2.代码 2.1 BaseMapper.java 把自动生成的方法都抽到一个base类,然后可以写一些公共的方法 /** * @author 吕梁山 * @date 2019/4/23 */ public i

  • PHP实现的生成唯一RequestID类完整示例

    本文实例讲述了PHP实现的生成唯一RequestID类.分享给大家供大家参考,具体如下: 这里介绍PHP生成唯一RequestID类,使用session_create_id()与uniqid()方法,保证唯一性,提供完整代码及演示,方便大家学习使用. 现在的系统设计一般使用分布式系统,一个请求可能要调用几个微服务处理,最后再把结果返回.当请求出现问题时,我们很难去跟踪是哪个微服务出现问题. 每个请求访问服务器时,我们可以给这个访问加入一个唯一标识(RequestID),在请求开始,请求过程中,及

  • JS密码生成与强度检测完整实例(附demo源码下载)

    本文实例讲述了JS密码生成与强度检测的方法.分享给大家供大家参考,具体如下: 1. 生成强密码 截图如下: 相关代码如下: function getPwd(n) { var s = ''; while(n--) s += String.fromCharCode(33 + Math.floor(Math.random()*(126-33))) document.getElementById('txt1').value = s; } 2. 计算密码破解时间 截图如下: 相关代码如下: functio

  • php实现带读写分离功能的MySQL类完整实例

    本文实例讲述了php实现带读写分离功能的MySQL类.分享给大家供大家参考,具体如下: 概述: 1. 根据sql语句判断是连接读库还是写库 2. 链式调用$this->where()->get() 3. 不同的主机对应不同的实例, 不再多次new 具体代码如下: <?php class DBRWmysql { private static $Instance = null; private $links = array();//链接数组 private $link = null; //当

  • php基于单例模式封装mysql类完整实例

    本文实例讲述了php基于单例模式封装mysql类.分享给大家供大家参考,具体如下: 类: <?php header("content-type:text/html;charset=utf-8"); //封装一个类 /* 掌握满足单例模式的必要条件 (1)私有的构造方法-为了防止在类外使用new关键字实例化对象 (2)私有的成员属性-为了防止在类外引入这个存放对象的属性 (3)私有的克隆方法-为了防止在类外通过clone成生另一个对象 (4)公有的静态方法-为了让用户进行实例化对象

  • JS实现点击生成UUID的方法完整实例【基于jQuery】

    本文实例讲述了JS实现点击生成UUID的方法.分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.jb51.net 用JS生成UUID的方法实例</title> <script src="http://libs.baidu.com/jquery/2.0

  • php实现解析xml并生成sql语句的方法

    本文实例讲述了php实现解析xml并生成sql语句的方法.分享给大家供大家参考,具体如下: php解析xml有很多种办法,文档中有,搜索一下就一大把的. 今天遇到一个需求:将某个xml中的节点属性提取出来,然后更新数据库某一表中的字段. 思路: 解析XML,获取所有的节点属性 –> 循环节点集合,获取对应的属性 –> 拼接sql字符串存入一数组 –> 将数组转为字符串保存于某一文件中 这里使用了xpath,在写代码的过程中遇到两个问题: 1.xml的史路径属性为D:\xx\-时load不

  • PHP+Mysql实现多关键字与多字段生成SQL语句的函数

    本文实例讲述了PHP+Mysql实现多关键字与多字段生成SQL语句的函数的方法.分享给大家供大家参考.具体实现方法如下: 先看实例: 复制代码 代码如下: $keyword="1 2 3"; echo $sql=search($keyword,"enter_gongyin_pic","a+b+c"); //函数生成,没有LIMIT,没有ORDER BY 生成: 复制代码 代码如下: SELECT * FROM `enter_gongyin_pic

随机推荐