php 数据库类

 更新时间:2016年11月25日 16:05  点击:1429

 +----------------------------------------------------------
         * @param mixed $where 数据
         * @param string $tables  数据表名
         * @param string $fields  字段名
         * @param string $order  排序
         * @param string $limit
         * @param string $group
         * @param string $having
         * @param boolean $cache 是否缓存
         * @param boolean $lazy 是否惰性加载
         * @param boolean $lock 是否加锁
         +----------------------------------------------------------
         * @return ArrayObject
         +----------------------------------------------------------
         * @throws ThinkExecption
         +----------------------------------------------------------
         */
        public function find($where,$tables,$fields='*',$order=null,$limit=null,$group=null,$having=null,$join=null,$cache=false,$lazy=false,$lock=false)
        {
                if(in_array($this->getDbType(),array('MSSQL','IBASE'),true) ) {
                        $this->queryStr = 'SELECT '.$this->parseLimit($limit)
                        .$this->parseFields($fields)
                        .' FROM '.$tables
                        .$this->parseJoin($join)
                        .$this->parseWhere($where)
                        .$this->parseGroup($group)
                        .$this->parseHaving($having)
                        .$this->parseOrder($order);
                }else{
                        $this->queryStr = 'SELECT '.$this->parseFields($fields)
                        .' FROM '.$tables
                        .$this->parseJoin($join)
                        .$this->parseWhere($where)
                        .$this->parseGroup($group)
                        .$this->parseHaving($having)
                        .$this->parseOrder($order);
                        if("ORACLE" == $this->getDbType())
                                if($limit[0] <= 0){
                                        if($limit[1] > 0)
                                                $this->queryStr = "SELECT * FROM (".$this->queryStr.") WHERE ROWNUM <= ".$limit[1];
                                }else{
                                        $whereClause = "";
                                        if($limit[1] > 0)
                                                $whereClause = " WHERE ROWNUM <= ".($limit[0] + $limit[1]);

                                        $this->queryStr = "SELECT * FROM ( SELECT ROW_.*, ROWNUM ROWNUM_ FROM ("
                                                                          .$this->queryStr.") ROW_"
                                                                          .$whereClause
                                                                          .") WHERE ROWNUM_ > "
                                                          .$limit[0];
                                }
                        else
                                $this->queryStr .= $this->parseLimit($limit);
                }
                return $this->query('',$cache,$lazy,$lock);
        }

require_once('cls.small_editor.php');
$obj = new SmallEditor();
$obj -> setEditor($e_form_name,$e_form_value,$width,$height);

<?php
        /*===========================================================
        = 版权协议:
        =        GPL (The GNU GENERAL PUBLIC LICENSE Version 2, June 1991)
        =------------------------------------------------------------
        = 文件名称:cls.small_editor.php
        = 摘    要:轻量级HTML编辑器 for PHP5
        = 版    本:1.0
        =------------------------------------------------------------
        = Script Written By phpwms项目组
        = 最后更新:xinge
        = 最后日期:2008-07-24
        ============================================================*/
        
        !defined('IN_SYS') and die('Hacking attempt');

        class SmallEditor {
               
                public $editor_dir = 'editor/SMALLeditor/';
               
                public function  __construct() {
                        global $root_dir;
                        $this -> editor_dir = $root_dir.$this -> editor_dir;
                        ob_start(array($this,'callBack'));
                }

                // 初始化
                private function editorInit() {
                        $str  = '<link rel="stylesheet" type="text/css" href="'.$this -> editor_dir.'comm.css" />'."\n";
                        $str .= '<script type="text/javascript" language="JavaScript" src="'.$this -> editor_dir.'all.js"></script>'."\n";
                        $str .= '<script type="text/javascript" language="JavaScript" src="'.$this -> editor_dir.'editor.js"></script>'."\n";
                        $str .= '<script type="text/javascript" language="JavaScript" src="'.$this -> editor_dir.'editor_toolbar.js"></script>'."\n";
                        return $str;
                }

                // 回调替换
                public function callBack($buffer) {
                        return (str_replace(array('</HEAD>','</Head>','</head>'),$this -> editorInit().'</HEAD>', $buffer));
                }

                public function setEditor($e_form_name = 'my_text_area',$e_form_value = '',$e_toolbar = 'AdminMode',$height='',$width = '100%') {
                        !$height and $height = 230;
                        !strstr($height,'%') and $height = $height.'px';
                        !strstr($width,'%')  and $width  = $width.'px';
                        return <<<EOF
                <textarea id="{$e_form_name}" name="{$e_form_name}" style="display:none;">{$e_form_value}</textarea>
                <script language="javascript">
                        gContentId = "{$e_form_name}";
                        OutputEditorLoading();
                </script>
                <iframe id="HtmlEditor" class="editor_frame" frameborder="0" marginheight="0" marginwidth="0" style="width:{$width};height:{$height};overflow:visible;" hideFocus></iframe>
EOF;
                }
        }
?>新浪编辑器的调用

jquery与 ajax 简单例子

基于JQuery框架的AJAX
PS:本人这篇始发于PHPCHINA,发现被很多人转了,但却未注明出处,想了一下,还是自己转到这里来。
前几天发了个贴,分享了prototype框架关于AJAX方面的学习过程。然后有人说jquery框架更方便。

正好项目中准备使用thickbox,于是干脆抛弃prototype.js,看起jquery.js了。JQuery确实不错,体积比Prototype小了许多,而且使用起来更方便更灵活。有人说Prototype像JAVA,正统;而JQuery像Ruby,灵活,更趋于OOP。

小试了下AJAX,感觉比prototype简洁多了,在JQuery中,AJAX有三种实现方式:$.ajax(),$.post,$.get()。

XHTML(主要):
代码: 复制内容到剪贴板
<div id="result" style="backgroundrange;border:1px solid red;width:300px;height:400px;"></div>

<form id="formtest" action="" method="post">

<p><span>输入姓名:</span><input type="text" name="username" id="input1" /></p>

<p><span>输入年龄:</span><input type="text" name="age" id="input2" /></p>

<p><span>输入性别:</span><input type="text" name="sex" id="input3" /></p>

<p><span>输入工作:</span><input type="text" name="job" id="input4" /></p>

</form>

<button id="send_ajax">提交</button>

<button id="test_post">OST提交</button>

<button id="test_get">GET提交</button>
JS:
1、引入jquery框架:
代码: 复制内容到剪贴板
<script  type="text/javascript" src="../js/jquery.js"></script>
2、构建AJAX,JQUERY的好处是不需要在XHTML中使用JS代码来触发事件了,可以直接封装在JS文件中:
代码: 复制内容到剪贴板
<script type="text/javascript">

//$.ajax()方式

$(document).ready(function (){

   $('#send_ajax').click(function (){ //直接把onclick事件写在了JS中,而不需要混在XHTML中了

          var params=$('input').serialize(); //序列化表单的值,与prototype中的form.serialize()相同

         $.ajax({

               url :'ajax_test.php',  //后台处理程序

               type:'post',    //数据发送方式

             dataType:'json',  //接受数据格式

               data:params,  //要传递的数据

               success:update_page  //回传函数(这里是函数名)

               });

        });

});

function update_page (json) { //回传函数实体,参数为XMLhttpRequest.responseText

       var str="姓名:"+json.username+"";

       str+="年龄:"+json.age+"";

       str+="性别:"+json.sex+"";

       str+="工作:"+json.job;

       $("#result").html(str);

}

//$.post()方式:

$(function (){//$(document).ready(function (){ 的简写

      $('#test_post').click(function (){

                $.post('ajax_test.php',

               {username('#input1').val(),age('#input2').val(),sex('#input3').val(),job('#input4').val()},

                function (data){ //回传函数

                var myjson='';

               eval('myjson='+data+';');

               $('#result').html("姓名:"+myjson.username+"

工作:"+myjson['job']);

               });

       });

});

$.get()方式:

$(function (){

         $('#test_get').click(function (){

                     $.get('ajax_test.php',

                     {username("#input1").val(),age("#input2").val(),sex("#input3").val(),job("#input4").val()},

                     function  (data) {

                           var myjson='';

                           eval("myjson="+data+";");

                           $("#result").html(myjson.job);

                      });

           });

});

</script>
PHP代码:
代码: 复制内容到剪贴板
<?php

$arr=$_POST; //若以$.get()方式发送数据,则要改成$_GET.或者干脆_REQUEST

$myjson=json_encode($arr);

echo $myjson;

?>


<?php session_start(); ?>
<?php
{
$authcode = new AuthCode();
if ($authcode->GetUriFileName() == "authcode.php")
{
  $authcode->OutputImg();
}
}
function ChkAuthcode($Authcode)
{
if ($_SESSION['AuthCode'] == $Authcode)
{
  $rtn = true;
}
else
{
  $rtn = false;
}
$_SESSION['AuthCode'] = rand(0, 999999);
return $rtn;
}
class AuthCode
{
/* Public Variables */
/* Private Variables */
var $image;
#
var $sBgcolor;
var $nWidth;
var $nHeight;
var $nLen;
var $bNoise;
var $nNoise;
var $bBorder;
var $aFontlist;
/* Constractor */
function AuthCode()
{
  $this->sBgcolor = "#FFCC00";
  $this->nWidth = 70;
  $this->nHeight = 25;
  $this->nLeftMargin = 5;
  $this->nRightMargin = 5;
  $this->nTopMargin = 3;
  $this->nBottomMargin = 2;
  $this->nLen = 4;
  $this->bNoise = true;
  $this->nNoisePoint = 50;
  $this->nNoiseLine = 5;
  $this->bBorder = true;
 
  $this->aFontlist[0] = "./fonts/arial.ttf";
  $this->aFontlist[1] = "./fonts/comic.ttf";
  $this->aFontlist[2] = "./fonts/raavi.ttf";
  $this->aFontlist[3] = "./fonts/verdanai.ttf";
  $this->aFontlist[4] = "./fonts/tahoma.ttf";
  $this->aFontlist[5] = "./fonts/shruti.ttf";
  $this->aFontlist[6] = "./fonts/BKANT.TTF";
  $this->aFontlist[7] = "./fonts/comicbd.ttf";
  $this->aFontlist[8] = "./fonts/courbi.ttf";
  $this->aFontlist[9] = "./fonts/times.ttf";
}

function OutputImg()
{
  $this->image = "";
  $this->image = imagecreate($this->nWidth, $this->nHeight);
  $back = $this->getcolor($this->sBgcolor);
  imagefilledrectangle($this->image, 0, 0, $this->nWidth, $this->nHeight, $back);
  $size = ($this->nWidth - $this->nLeftMargin - $this->nRightMargin)/$this->nLen;
  if($size>($this->nHeight - $this->nTopMargin - $this->nBottomMargin))
   $size=$this->nHeight - $this->nTopMargin - $this->nBottomMargin;
 
  $left = ($this->nWidth-$this->nLen*($size+$size/10))/2 + $this->nLeftMargin;
  $code = "";
  for ($i=0; $i<$this->nLen; $i++)
  {
   $randtext = rand(0, 9);
   $code .= $randtext;
   $textColor = imagecolorallocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
   $font = $this->aFontlist[rand(0,9)];//rand(1,4).".ttf";
   $randsize = rand($size-$size/10, $size+$size/10);
   $location = $left+($i*$size+$size/10);
   imagettftext($this->image, $randsize, rand(-18,18), $location, rand($size, $size+$size/5) + $this->nTopMargin, $textColor, $font, $randtext);
  }
  if($this->bNoise == true) $this->setnoise();
  $_SESSION['AuthCode'] = $code;
  $bordercolor = $this->getcolor("      ");
  if($border==true) imagerectangle($this->image, 0, 0, $this->nWidth-1, $this->nHeight-1, $bordercolor);
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // date in the past
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Cache-Control: private");
  header("Pragma: no-cache"); // HTTP/1.0
  header("Content-type: image/png");
  imagepng($this->image);
  imagedestroy($this->image);
 
  return $sAuthcode;
}
function ChkAuthcode($Authcode)
{
  if ($this->GetAuthcode() == $Authcode)
  {
   $rtn = true;
  }
  else
  {
   $rtn = false;
  }
  $_SESSION['AuthCode'] = rand(0, 999999);
  return $rtn;
}
function GetAuthcode()
{
  $x_AuthCode = $_SESSION['AuthCode'];
  $_SESSION['AuthCode'] = rand(0, 999999);
  return $x_AuthCode;
}
 
/* Private Functions */
function GetUriFileName()
{
  return substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], '/') + 1 , strlen($_SERVER['SCRIPT_NAME']) - strrpos($_SERVER['SCRIPT_NAME'], '/'));
}
function setnoise()
{
  for ($i=0; $i<$this->nNoiseLine; $i++){
   $randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
   imageline($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);
  }
 
  for ($i=0; $i<$this->nNoisePoint; $i++){
   $randColor = imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)); 
   imagesetpixel($this->image, rand(0, $this->nWidth), rand(0, $this->nHeight), $randColor);
  }
}
function getcolor($color)
{
   $color = eregi_replace ("^#","",$color);
   $r = $color[0].$color[1];
   $r = hexdec ($r);
   $b = $color[2].$color[3];
   $b = hexdec ($b);
   $g = $color[4].$color[5];
   $g = hexdec ($g);
   $color = imagecolorallocate ($this->image, $r, $b, $g);
   return $color;
}
}
?>
function backup($bakfile = null, $tables = array())
 {
  if (empty($bakfile)) {
   $bakfile = $this->dbname . date("Ymdhis") . '.sql';
  } elseif (is_dir($bakfile)) {
   if (preg_match('//$/', $bakfile)) {
    $bakfile = $bakfile . $this->dbname . date("Ymdhis") . '.sql';
   } else {
    $bakfile = $bakfile . '/' . $this->dbname . date("Ymdhis") . '.sql';
   }
  }
  if (!$tables) {
   $this->query("SHOW TABLES");
   while ($row = mysql_fetch_row($this->queryID)) {
    $tables[] = $row[0];
   }
  } else {
   foreach ($tables as $k => $v) {
    $tables[$k] = $this->tablePrefix . $v;
   }
  }
 
  if ($fp = fopen($bakfile, 'wb')) {
   if ($this->dbcharset) {
    fwrite($fp, "SET NAMES " . $this->dbcharset . ";nn");
   }
   foreach ($tables as $table) {
    $this->dumpTable($table, $fp);
    fwrite($fp, "n");
   }//foreach
   fclose($fp);
   return true;
  } else {
   return false;
  }//if
 }
 
 //私有方法 导出表格
 function dumpTable($fullTableName, $fp)
 {
  //备份表结构 
  fwrite($fp, "-- n-- {$fullTableName}n-- n");
  $row = $this->findBySql("SHOW CREATE TABLE `{$fullTableName}`");
  fwrite($fp, $row['Create Table'] . ";nn" );
  //备份表库数据
  $this->query("SELECT * FROM `{$fullTableName}`");
  while ($row = mysql_fetch_assoc($this->queryID)) {
   foreach ($row as $k=>$v) {
    $row[$k] = "'" . $this->qstr($v) . "'" ;
   }
   $sql = "INSERT INTO `$fullTableName` VALUES (" . join(",", $row) . ");n";
   fwrite($fp, $sql);
  }
  mysql_free_result($this->queryID);
  fwrite($fp, "n");
 }
 
 //恢复数据库文件
 function restore($bakfile)
 {
  if ($fp = fopen($bakfile, 'r')) {
   $sql = '';
   while (!feof($fp)) {
    $line = fgets($fp);
    if (strpos($line,'--')!==0)
    {
     $sql .= $line;
     //pp($sql);
    }
    if (preg_match('/;s*$/', $sql)) {
     $this->query($sql);
     $sql = '';
    }
   }
   fclose($fp);
   return true;
  } else {
   return false;
  }
 }
[!--infotagslink--]

相关文章

  • PHP 数据库缓存Memcache操作类

    操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • php简单数据操作的实例

    最基础的对数据的增加删除修改操作实例,菜鸟们收了吧...2013-09-26
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • 解决Mybatis 大数据量的批量insert问题

    这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
  • Antd-vue Table组件添加Click事件,实现点击某行数据教程

    这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17
  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • 详解如何清理redis集群的所有数据

    这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
  • Intellij IDEA连接Navicat数据库的方法

    这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
  • 在数据库里将毫秒转换成date格式的方法

    在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • vue 获取到数据但却渲染不到页面上的解决方法

    这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 如何解决局域网内mysql数据库连接慢

    通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
  • php把读取xml 文档并转换成json数据代码

    在php中解析xml文档用专门的函数domdocument来处理,把json在php中也有相关的处理函数,我们要把数据xml 数据存到一个数据再用json_encode直接换成json数据就OK了。...2016-11-25
  • mybatis-plus 处理大数据插入太慢的解决

    这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
  • MySQL快速复制数据库数据表的方法

    某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
  • mysqldump命令导入导出数据库方法与实例汇总

    mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
  • Mysql数据库错误代码中文详细说明

    1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
  • postgresql数据添加两个字段联合唯一的操作

    这篇文章主要介绍了postgresql数据添加两个字段联合唯一的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04