仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 867|回复: 18
打印 上一主题 下一主题

[学习教程] PHP网页设计在PHPLIB中的MYSQL类中加INSERT,UPDATE...

[复制链接]
透明 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:57:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
掌握静态网页的制作技术是学习开发网站的先决条件,这一点就讲到这里,因为这篇文章不是教程文章,也就不对技术进行深入的刨析了。   <?php
/*
* Session Management for PHP3
*
* Copyright (c) 1998-2000 NetUSE AG
*                    Boris Erdmann, Kristian Koehntopp
*
* $Id: db_mysql.inc,v 1.2 2000/07/12 18:22:34 kk Exp $
*
*/
class DB_Sql {
  
  /* public: connection parameters */
  var $Host     = "";
  var $Database = "";
  var $User     = "";
  var $Password = "";
  /* public: configuration parameters */
  var $Auto_Free     = 0;     ## Set to 1 for automatic mysql_free_result()
  var $Debug         = 0;     ## Set to 1 for debugging messages.
  var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
  var $Seq_Table     = "db_sequence";
  /* public: result array and current row number */
  var $Record   = array();
  var $Row;
  /* public: current error number and error text */
  var $Errno    = 0;
  var $Error    = "";
  /* public: this is an api revision, not a CVS revision. */
  var $type     = "mysql";
  var $revision = "1.2";
  /* private: link and query handles */
  var $Link_ID  = 0;
  var $Query_ID = 0;
  
  var $sql = "";
  /* public: constructor */
  function DB_Sql($query = "") {
      $this->query($query);
  }
  /* public: some trivial reporting */
  function link_id() {
    return $this->Link_ID;
  }
  function query_id() {
    return $this->Query_ID;
  }
  /* public: connection management */
  function connect($Database = "", $Host = "", $User = "", $Password = "") {
    /* Handle defaults */
    if ("" == $Database)
      $Database = $this->Database;
    if ("" == $Host)
      $Host     = $this->Host;
    if ("" == $User)
      $User     = $this->User;
    if ("" == $Password)
      $Password = $this->Password;
      
    /* establish connection, select database */
    if ( 0 == $this->Link_ID ) {
   
      $this->Link_ID=mysql_pconnect($Host, $User, $Password);
      if (!$this->Link_ID) {
        $this->halt("pconnect($Host, $User, \$Password) failed.");
        return 0;
      }
      if (!@mysql_select_db($Database,$this->Link_ID)) {
        $this->halt("cannot use database ".$this->Database);
        return 0;
      }
    }
   
    return $this->Link_ID;
  }
  /* public: discard the query result */
  function free() {
      @mysql_free_result($this->Query_ID);
      $this->Query_ID = 0;
  }
  /* public: perform a query */
  function query($Query_String) {
    /* No empty queries, please, since PHP4 chokes on them. */
    if ($Query_String == "")
      /* The empty query string is passed on from the constructor,
       * when calling the class without a query, e.g. in situations
       * like these: '$db = new DB_Sql_Subclass;'
       */
      return 0;
    if (!$this->connect()) {
      return 0; /* we already complained in connect() about that. */
    };
    # New query, discard previous result.
    if ($this->Query_ID) {
      $this->free();
    }
    if ($this->Debug)
      printf("Debug: query = %s<br>\n", $Query_String);
    $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
    $this->Row   = 0;
    $this->Errno = mysql_errno();
    $this->Error = mysql_error();
    if (!$this->Query_ID) {
      $this->halt("Invalid SQL: ".$Query_String);
    }
    # Will return nada if it fails. That's fine.
    return $this->Query_ID;
  }
  /* public: walk result set */
  function next_record() {
    if (!$this->Query_ID) {
      $this->halt("next_record called with no query pending.");
      return 0;
    }
    $this->Record = @mysql_fetch_array($this->Query_ID);
    $this->Row   += 1;
    $this->Errno  = mysql_errno();
    $this->Error  = mysql_error();
    $stat = is_array($this->Record);
    if (!$stat && $this->Auto_Free) {
      $this->free();
    }
    return $stat;
  }
  
  /* public: insert record */
  function insert( $table, $data) {
    if ( is_array( $data ) ) {
  $this->sql = "INSERT INTO $table ( `".implode( "` , `" , array_keys($data) )."` )
    VALUES ( '".implode( "' , '", array_values($data) )."' )";
} else {
  die("<strong>Error</strong> : Data Empty! ");
}
return $this->query( $this->sql );
  }
  
  /* public: delete record */
  function delete( $table, $where = "", $limit = 0 ) {
$this->sql  = "DELETE FROM $table ";
$this->sql .= $this->where($where);
$this->sql .= intval($limit) ? " LIMIT ". intval($limit) : "";
return $this->query( $this->sql );
  }
  
  /* public: update record */
  function update( $table, $update = "", $where = "", $limit = 0 ) {
    $setword = "";
if( is_array( $update ) ) {
  while ( list( $field, $value ) = each($update) ) {
   $setword .= $setword ? " , " : "";
   $setword .= "`$field` = '".$value."'";
  }
}else {
  $setword = $update;
}

$this->sql  = "UPDATE $table SET $setword ";
$this->sql .= $this->where($where);
$this->sql .= intval($limit) ? " LIMIT ". intval($limit) : "";
return $this->query( $this->sql );
  }
  
  
  /* public: count record */
  function recordcount( $table, $where = "" ) {
    $this->sql = "SELECT COUNT(*) FROM $table " . $this->where($where);
$this->query( $this->sql );
if( $this->next_record() ) {
  return $this->f(0);
}else {
  return 0;
}
  }
  
  /* public: select record */
  function select( $table, $fields = "*", $where = "", $order = "", $group = "", $limit = 10 ) {
   
  }
  
  /* public: set where */
  function where( $where ) {
   $condition = "";
if( is_array( $where ) ) {
  foreach( $where as $key => $val ){
   if( preg_match( "/^\d*$/", $key ) ) {//or
    foreach( $val as $k => $v ) {
     $condition .= $condition
         ? " OR ( `$k` = '".implode( "' AND `$k` = '", $v )."' )"
         : " WHERE ( `$k` = '".implode( "' AND `$k` = '", $v )."')";
    }
   }else {//and
    $condition .= $condition
        ? " AND ( `$key` = '".implode( "' OR `$key` = '", $val )."')"
        : " WHERE ( `$key` = '".implode( "' OR `$key` = '", $val )."')";
   }
  }
}else {
  $condition .= " ".$where;
}
    return $condition;
  }
  
  /* public: show sql */
  function showsql() {
    return $this->sql;
  }

  
  /* public: position in result set */
  function seek($pos = 0) {
    $status = @mysql_data_seek($this->Query_ID, $pos);
    if ($status)
      $this->Row = $pos;
    else {
      $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
      /* half assed attempt to save the day,
       * but do not consider this documented or even
       * desireable behaviour.
       */
      @mysql_data_seek($this->Query_ID, $this->num_rows());
      $this->Row = $this->num_rows;
      return 0;
    }
    return 1;
  }
  /* public: table locking */
  function lock($table, $mode="write") {
    $this->connect();
   
    $query="lock tables ";
    if (is_array($table)) {
      while (list($key,$value)=each($table)) {
        if ($key=="read" && $key!=0) {
          $query.="$value read, ";
        } else {
          $query.="$value $mode, ";
        }
      }
      $query=substr($query,0,-2);
    } else {
      $query.="$table $mode";
    }
    $res = @mysql_query($query, $this->Link_ID);
    if (!$res) {
      $this->halt("lock($table, $mode) failed.");
      return 0;
    }
    return $res;
  }
  
  function unlock() {
    $this->connect();
    $res = @mysql_query("unlock tables");
    if (!$res) {
      $this->halt("unlock() failed.");
      return 0;
    }
    return $res;
  }

  /* public: evaluate the result (size, width) */
  function affected_rows() {
    return @mysql_affected_rows($this->Link_ID);
  }
  function num_rows() {
    return @mysql_num_rows($this->Query_ID);
  }
  function num_fields() {
    return @mysql_num_fields($this->Query_ID);
  }
  /* public: shorthand notation */
  function nf() {
    return $this->num_rows();
  }
  function np() {
    print $this->num_rows();
  }
  function f($Name) {
    return $this->Record[$Name];
  }
  function p($Name) {
    print $this->Record[$Name];
  }
  /* public: sequence numbers */
  function nextid($seq_name) {
    $this->connect();
   
    if ($this->lock($this->Seq_Table)) {
      /* get sequence number (locked) and increment */
      $q  = sprintf("select nextid from %s where seq_name = '%s'",
                $this->Seq_Table,
                $seq_name);
      $id  = @mysql_query($q, $this->Link_ID);
      $res = @mysql_fetch_array($id);
      
      /* No current value, make one */
      if (!is_array($res)) {
        $currentid = 0;
        $q = sprintf("insert into %s values('%s', %s)",
                 $this->Seq_Table,
                 $seq_name,
                 $currentid);
        $id = @mysql_query($q, $this->Link_ID);
      } else {
        $currentid = $res["nextid"];
      }
      $nextid = $currentid + 1;
      $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
               $this->Seq_Table,
               $nextid,
               $seq_name);
      $id = @mysql_query($q, $this->Link_ID);
      $this->unlock();
    } else {
      $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
      return 0;
    }
    return $nextid;
  }
  /* public: return table metadata */
  function metadata($table='',$full=false) {
    $count = 0;
    $id    = 0;
    $res   = array();
    /*
     * Due to compatibility problems with Table we changed the behavior
     * of metadata();
     * depending on $full, metadata returns the following values:
     *
     * - full is false (default):
     * $result[]:
     *   [0]["table"]  table name
     *   [0]["name"]   field name
     *   [0]["type"]   field type
     *   [0]["len"]    field length
     *   [0]["flags"]  field flags
     *
     * - full is true
     * $result[]:
     *   ["num_fields"] number of metadata records
     *   [0]["table"]  table name
     *   [0]["name"]   field name
     *   [0]["type"]   field type
     *   [0]["len"]    field length
     *   [0]["flags"]  field flags
     *   ["meta"][field name]  index of field named "field name"
     *   The last one is used, if you have a field name, but no index.
     *   Test:  if (isset($result['meta']['myfield'])) { ...
     */
    // if no $table specified, assume that we are working with a query
    // result
    if ($table) {
      $this->connect();
      $id = @mysql_list_fields($this->Database, $table);
      if (!$id)
        $this->halt("Metadata query failed.");
    } else {
      $id = $this->Query_ID;
      if (!$id)
        $this->halt("No query specified.");
    }

    $count = @mysql_num_fields($id);
    // made this IF due to performance (one if is faster than $count if's)
    if (!$full) {
      for ($i=0; $i<$count; $i++) {
        $res[$i]["table"] = @mysql_field_table ($id, $i);
        $res[$i]["name"]  = @mysql_field_name  ($id, $i);
        $res[$i]["type"]  = @mysql_field_type  ($id, $i);
        $res[$i]["len"]   = @mysql_field_len   ($id, $i);
        $res[$i]["flags"] = @mysql_field_flags ($id, $i);
      }
    } else { // full
      $res["num_fields"]= $count;
   
      for ($i=0; $i<$count; $i++) {
        $res[$i]["table"] = @mysql_field_table ($id, $i);
        $res[$i]["name"]  = @mysql_field_name  ($id, $i);
        $res[$i]["type"]  = @mysql_field_type  ($id, $i);
        $res[$i]["len"]   = @mysql_field_len   ($id, $i);
        $res[$i]["flags"] = @mysql_field_flags ($id, $i);
        $res["meta"][$res[$i]["name"]] = $i;
      }
    }
   
    // free the result only if we were called on a table
    if ($table) @mysql_free_result($id);
    return $res;
  }
  /* private: error handling */
  function halt($msg) {
    $this->Error = @mysql_error($this->Link_ID);
    $this->Errno = @mysql_errno($this->Link_ID);
    if ($this->Halt_On_Error == "no")
      return;
    $this->haltmsg($msg);
    if ($this->Halt_On_Error != "report")
      die("Session halted.");
  }
  function haltmsg($msg) {
    printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
    printf("<b>MySQL Error</b>: %s (%s)<br>\n",
      $this->Errno,
      $this->Error);
  }
  function table_names() {
    $this->query("SHOW TABLES");
    $i=0;
    while ($info=mysql_fetch_row($this->Query_ID))
     {
      $return[$i]["table_name"]= $info[0];
      $return[$i]["tablespace_name"]=$this->Database;
      $return[$i]["database"]=$this->Database;
      $i++;
     }
   return $return;
  }
}
?>
你的确对PHP有兴趣,那么选择教材也是很重要的。
灵魂腐蚀 该用户已被删除
沙发
发表于 2015-2-4 07:18:48 | 只看该作者
,熟悉html,能用div+css,还有javascript,优先考虑linux。我在开始学习的时候,就想把这些知识一起学习,我天真的认为同时学习能够互相呼应,因为知识是相通的。
莫相离 该用户已被删除
板凳
发表于 2015-2-9 18:42:32 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
飘飘悠悠 该用户已被删除
地板
发表于 2015-2-27 16:50:55 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
再现理想 该用户已被删除
5#
发表于 2015-3-6 12:57:12 | 只看该作者
,熟悉html,能用div+css,还有javascript,优先考虑linux。我在开始学习的时候,就想把这些知识一起学习,我天真的认为同时学习能够互相呼应,因为知识是相通的。
海妖 该用户已被删除
6#
发表于 2015-3-11 04:50:36 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
小女巫 该用户已被删除
7#
发表于 2015-3-17 20:52:03 | 只看该作者
当然这种网站的会员费就几十块钱。
小魔女 该用户已被删除
8#
发表于 2015-3-21 19:31:30 | 只看该作者
开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。
冷月葬花魂 该用户已被删除
9#
发表于 2015-3-25 22:11:14 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
蒙在股里 该用户已被删除
10#
发表于 2015-4-3 01:12:07 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
愤怒的大鸟 该用户已被删除
11#
发表于 2015-4-11 03:37:36 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
因胸联盟 该用户已被删除
12#
发表于 2015-4-15 10:31:42 | 只看该作者
其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。
兰色精灵 该用户已被删除
13#
发表于 2015-4-17 09:08:29 | 只看该作者
爱上php,他也会爱上你。
变相怪杰 该用户已被删除
14#
发表于 2015-5-6 10:09:34 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
老尸 该用户已被删除
15#
发表于 2015-5-8 05:13:47 | 只看该作者
刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。
透明 该用户已被删除
16#
 楼主| 发表于 2015-5-12 02:48:16 | 只看该作者
你很难利用原理去编写自己的代码。对于php来说,系统的学习我认为还是很重要的,当你有一定理解后,你可你针对某种效果研究,我想那时你不会只是复制代码的水平了。
小妖女 该用户已被删除
17#
发表于 2015-6-23 20:41:00 | 只看该作者
基础有没有对学习php没有太大区别,关键是兴趣。
飘灵儿 该用户已被删除
18#
发表于 2015-6-27 23:21:53 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
谁可相欹 该用户已被删除
19#
发表于 2015-7-2 23:46:23 | 只看该作者
开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-30 13:02

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表