php mysql数据保存实例

 更新时间:2016年11月25日 15:53  点击:1749
这是一款老外写的mysql php接受数据过来然后进行数据保存在mysql_real_escape_string(nl2br(strip_tags($_POST[\'comment\'])));我觉得写得我们有一点区别吧。

database config
 */

 代码如下 复制代码

$db_host  = 'localhost';
$db_user  = 'root';
$db_pass  = '';
$db_database = '';

$link = mysql_connect($db_host,$db_user,$db_pass) or die('unable to establish a db connection');

mysql_select_db($db_database,$link);
mysql_query("set names utf8");


if(empty($_post['comment'])) die("0");
// if there isn't a comment text, exit

$comment = mysql_real_escape_string(nl2br(strip_tags($_post['comment'])));
$user='demo';
// this would be a nice place to start customizing - the default user
// you can integrate it to any site and show a different username.

$addon='';
if($_post['parent']) $addon=',parent='.(int)$_post['parent'];

mysql_query("insert into wave_comments set usr='".$user."', comment='".$comment."', dt=now()".$addon);

if(mysql_affected_rows($link)==1)
 echo mysql_insert_id($link);
 // if the insert was successful, echo the newly assigned id
else
 echo '0';
?>

sql

--
-- table structure for table `wave_comments`
--

create table `wave_comments` (
  `id` int(11) not null auto_increment,
  `parent` int(11) not null default '0',
  `usr` varchar(16) collate utf8_unicode_ci not null default '',
  `comment` text collate utf8_unicode_ci not null,
  `dt` datetime not null default '0000-00-00 00:00:00',
  primary key  (`id`),
  key `parent` (`parent`,`id`)
) engine=myisam  default charset=utf8 collate=utf8_unicode_ci;

--
-- dumping data for table `wave_comments`
--

insert into `wave_comments` values(1, 0, 'tutorialzine', 'this is a demo for a tutorialzine tutorial about creating a google wave-like history slider.<br /><br />rnto get started, just drag the slider above, and this thread will be reverted to a past state.', '2009-10-24 03:58:08');
insert into `wave_comments` values(2, 0, 'curious', 'is html allowed in the comments?', '2009-10-24 03:59:44');
insert into `wave_comments` values(3, 2, 'tutorialzine', 'nope. also the messages in this demo are deleted every hour to prevent spamming.', '2009-10-24 04:00:15');
insert into `wave_comments` values(4, 1, 'tutorialzine', 'in this tutorial we are using <b>php</b>, <b>mysql</b>, <b>jquery</b> and <b>css教程</b>. the slider was created with <b>jquery ui</b>. <a href="http://111cn.net/2009/10/google-wave-history-slider-jquery/" target="_blank">view the tutorial</a>.', '2009-10-24 04:01:34');
insert into `wave_comments` values(5, 2, 'curious', 'thanks! also i noticed that you can click, rather than drag the slider.great!', '2009-10-24 04:11:48');

这里把数据库中的常用的内容保存到一个标准的php格式的文件当中,这样使用起来也方便了很多,下面代码。

$res=mysql教程_query("select k1,k2  from ".table('keywords')." ") ;
$str="<?php rn ";
while($rs=mysql_fetch_array($res))
{
 
 $str .="$keyword['".$rs[0]."']='".$rs[1]."';rn";
}
$str.="?>";
file_put_contents("keyword.php",$str);
echo "导出成功";

//方法二

$f=file_get_contents("w1.txt");
$f=str_replace("rn","<br>",$f);//替换换行符
$arr=explode("<br>",$f);
$str="<?php rn ";
foreach($arr as $t)
{
 $rs=explode("|",$t);
 //不能包含?等特殊符号
 $str .="$keyword['".str_replace("?","",$rs[0])."']='".str_replace("?","",$rs[1])."';rn";
 
}
$str.="?>";
file_put_contents("keyword.php",$str);
echo "导出成功";

//conn.php文件
$conn=mysql_connect($mysql_host,$mysql_user,$mysql_password) or die('连接服务器出错');
mysql_select_db($mysql_db) or die("选择数据库出错");
mysql_query("set names 'gbk'");
function table($t)
{
 global $mysql_table_prefix;
 return $mysql_table_prefix.$t;
}
?>

本文章是利用了js php ajax css实现的一款可刷新的js 树形菜单,如果你正在找这类型的类型的树形菜单可以进来免费下载。
 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.111cn.net/tr/xhtml1/dtd/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>simpletree drag</title>
<style>
body
{
 font: normal 12px arial, tahoma, helvetica, sans-serif;
 margin:0;
 padding:20px;
}
.simpletree
{
 
 margin:0;
 padding:0;
 /*
 overflow:auto;
 width: 250px;
 height:350px;
 overflow:auto;
 border: 1px solid #444444;
 */
}
.simpletree li
{
 list-style: none;
 margin:0;
 padding:0 0 0 34px;
 line-height: 14px;
}
.simpletree li span
{
 display:inline;
 clear: left;
 white-space: nowrap;
}
.simpletree ul
{
 margin:0;
 padding:0;
}
.simpletree .root
{
 margin-left:-16px;
 background: url(images/root.gif) no-repeat 16px 0 #ffffff;
}
.simpletree .line
{
 margin:0 0 0 -16px;
 padding:0;
 line-height: 3px;
 height:3px;
 font-size:3px;
 background: url(images/line_bg.gif) 0 0 no-repeat transparent;
}
.simpletree .line-last
{
 margin:0 0 0 -16px;
 padding:0;
 line-height: 3px;
 height:3px;
 font-size:3px;
 background: url(images/spacer.gif) 0 0 no-repeat transparent;
}
.simpletree .line-over
{
 margin:0 0 0 -16px;
 padding:0;
 line-height: 3px;
 height:3px;
 font-size:3px;
 background: url(images/line_bg_over.gif) 0 0 no-repeat transparent;
}
.simpletree .line-over-last
{
 margin:0 0 0 -16px;
 padding:0;
 line-height: 3px;
 height:3px;
 font-size:3px;
 background: url(images/line_bg_over_last.gif) 0 0 no-repeat transparent;
}
.simpletree .folder-open
{
 margin-left:-16px;
 background: url(images/collaps教程able.gif) 0 -2px no-repeat #fff;
}
.simpletree .folder-open-last
{
 margin-left:-16px;
 background: url(images/collapsable-last.gif) 0 -2px no-repeat #fff;
}
.simpletree .folder-close
{
 margin-left:-16px;
 background: url(images/expandable.gif) 0 -2px no-repeat #fff;
}
.simpletree .folder-close-last
{
 margin-left:-16px;
 background: url(images/expandable-last.gif) 0 -2px no-repeat #fff;
}
.simpletree .doc
{
 margin-left:-16px;
 background: url(images/leaf.gif) 0 -1px no-repeat #fff;
}
.simpletree .doc-last
{
 margin-left:-16px;
 background: url(images/leaf-last.gif) 0 -1px no-repeat #fff;
}
.simpletree .ajax
{
 background: url(images/spinner.gif) no-repeat 0 0 #ffffff;
 height: 16px;
 display:none;
}
.simpletree .ajax li
{
 display:none;
 margin:0;
 padding:0;
}
.simpletree .trigger
{
 display:inline;
 margin-left:-32px;
 width: 28px;
 height: 11px;
 cursor:pointer;
}
.simpletree .text
{
 cursor: default;
}
.simpletree .active
{
 cursor: default;
 background-color:#f7be77;
 padding:0px 2px;
 border: 1px dashed #444;
}
#drag_container
{
 background:#ffffffwww.111cn.net;
 color:#000;
 font: normal 11px arial, tahoma, helvetica, sans-serif;
 border: 1px dashed #767676;
}
#drag_container ul
{
 list-style: none;
 padding:0;
 margin:0;
}

#drag_container li
{
 list-style: none;
 background-color:#ffffff;
 line-height:18px;
 white-space: nowrap;
 padding:1px 1px 0px 16px;
 margin:0;
}
#drag_container li span
{
 padding:0;
}

#drag_container li.doc, #drag_container li.doc-last
{
 background: url(images/leaf.gif) no-repeat -17px 0 #ffffff;
}
#drag_container .folder-close, #drag_container .folder-close-last
{
 background: url(images/expandable.gif) no-repeat -17px 0 #ffffff;
}

#drag_container .folder-open, #drag_container .folder-open-last
{
 background: url(/http://mb.111cn.net/collapsable.gif) no-repeat -17px 0 #ffffff;
}
.contextmenu
{
 display:none;
}
</style>
<script type="text/网页特效" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.simple.tree.js"></script>
<script type="text/javascript">
var simpletreecollection;
$(document).ready(function(){
 simpletreecollection = $('.simpletree').simpletree({
  autoclose: true,
  afterclick:function(node){
   //alert("text-"+$('span:first',node).text());
  },
  afterdblclick:function(node){
   //alert("text-"+$('span:first',node).text());
  },
  aftermove:function(destination, source, pos){
   //alert("destination-"+destination.attr('id')+" source-"+source.attr('id')+" pos-"+pos);
  },
  afterajax:function()
  {
   //alert('loaded');
  },
  animate:true
  //,doctofolderconvert:true
 });
});
</script>
</head>

<body>
<div class="contextmenu" id="mymenu1">
 <ul>
  <li id="add"><img src="images/folder_add.png" /> add child</li>
  <li id="reload"><img src="images/arrow_refresh.png" /> reload</li>
  <li id="edit"><img src="images/folder_edit.png" /> edit</li>
  <li id="delete"><img src="images/folder_delete.png" /> delete</li>
 </ul>
</div>
<div class="contextmenu" id="mymenu2">
 <ul>
  <li id="edit"><img src="images/page_edit.png" /> edit</li>
  <li id="delete"><img src="images/page_delete.png" /> delete</li>
 </ul>
</div>
<ul class="simpletree">
 <li class="root" id='1'><span>tree root 1</span>
  <ul>
   
   <li class="open" id='2'><span>tree node 1</span>
    <ul>
     
     <li id='3'><span>tree node 1-1</span>
      <ul class="ajax">
       <li id='4'>{url:loadtree.php?tree_id=1}</li>
      </ul>
     </li>
     
    </ul>
   </li>
   
   <li id='5'><span>tree node 2</span>
    <ul>
     
     <li id='6'><span>tree node 2-1</span>
      <ul>
       
       <li id='7'><span>tree node 2-1-1</span></li>
       
       <li id='8'><span>tree node 2-1-2</span></li>
       
       <li id='9'><span>tree node 2-1-3</span></li>
       
       <li id='10'><span>tree node 2-1-4www.aimeige.com.cn</span>
        <ul class="ajax">
         <li id='11'>{url:loadtree.php?tree_id=1}</li>
        </ul>
       </li>
       
      </ul>
     </li>
     
     <li id='12'><span>tree node 2-2</span>
      <ul>
       
       <li id='13'><span>tree node 2-2-1</span></li>
       
      </ul>
     </li>
     
     
     <li id='14'><span>tree node 2-3</span>
      <ul>
       
       <li id='15'><span>tree node 2-3-1</span>
         <ul>
          
          <li id='16'><span>tree node 2-3-1-1</span></li>
          
          <li id='17'><span>tree node 2-3-1-2</span></li>
          
          <li id='18'><span>tree node 2-3-1-3</span>
           <ul>
            
            <li id='19'><span>tree node 2-3-1-3-1</span></li>
            
           </ul>
          </li>
          
          <li id='20'><span>tree node 2-3-1-4</span></li>
          
          <li id='21'><span>tree node 2-3-1-5</span></li>
          
          <li id='22'><span>tree node 2-3-1-6</span>
           <ul>
            
            <li id='23'><span>tree node 2-3-1-6-1</span></li>
            
           </ul>
          </li>
          
          <li id='24'><span>tree node 2-3-1-7</span></li>
          
          <li id='25'><span>tree node 2-3-1-8</span></li>
          
          <li id='26'><span>tree node 2-3-1-9</span>
           <ul>
            
            <li id='27'><span>tree node 2-3-1-9-1</span></li>
            
           </ul>
          </li>
          
         </ul>
       </li>
       
      </ul>
     </li>
     
    </ul>
   </li>
   
  </ul>
 </li>
</ul>

</body>

</html>

php文件
<li id='35'><span class="text">tree node ajax 1</span></li>
<li id='36'><span class="text">tree node ajax 2</span></li>
<li id='37'><span class="text">tree node ajax 3</span>
 <ul>
  <li id='38'><span class="text">tree node ajax 3-1</span>
   <ul>
    <li id='39'><span class="text">tree node ajax 3-1-1</span></li>
    <li id='40'><span class="text">tree node ajax 3-1-2</span></li>
    <li id='41'><span class="text">tree node ajax 3-1-3</span></li>
    <li id='42'><span class="text">tree node ajax 3-1-4</span></li>
   </ul>
  </li>
  <li id='43'><span class="text">tree node ajax 3-2</span></li>
  <li id='44'><span class="text">tree node ajax 3-3</span>
   <ul>
    <li id='45'><span class="text">tree node ajax 3-3-1</span></li>
    <li id='46'><span class="text">tree node ajax 3-3-2</span></li>
    <li id='47'><span class="text">tree node ajax 3-3-3</span></li>
   </ul>
  </li>
  <li id='48'><span class="text">tree node ajax 3-4</span></li>
  <li id='49'><span class="text">tree node ajax 3-5</span>
   <ul>
    <li id='50'><span class="text">tree node ajax 3-5-1</span></li>
    <li id='51'><span class="text">tree node ajax 3-5-2</span></li>
    <li id='52'><span class="text">tree node ajax 3-5-3</span></li>
   </ul>
  </li>
  <li id='53'><span class="text">tree node ajax 3-6</span></li>
 </ul>
</li>
<li id='54'><span class="text">tree node ajax 4</span></li>

源码下载
http://down.111cn.net/down/code/jquery/2010/1014/21200.html
效果预览
http://g.111cn.net/javascript/code/20101012/tree

本文章收藏了一款简单php文件上传实例哦,这真的是一款超级简单的文件上传功能代码哦,我们利用php $files来获取要上传文件的,类型,名称与临时名称然后用move_uploaded_file把要上传文件保存到服务器指定目录就OK了。

html代码

 代码如下 复制代码
<input   type= "file "   id= "userfile "   name= "userfile ">
<input   type= "submit "   name= "upload "   value= "上传 ">

处理上传php代码

 

 代码如下 复制代码
<?php
function   do_upload($upload_dir,$upload_url)
    {
          $temp_name   =   $_files[ 'userfile '][ 'tmp_name '];
          $file_name   =   $_files[ 'userfile '][ 'name '];
          $file_name   =   str_replace( "\ ", " ",$file_name);
          $file_name   =   str_replace( " ' ", " ",$file_name);
          $file_path   =   $upload_dir.$file_name;
          $thistime=explode( "- ",date( "y-m-d-h-i-s "));
          $thistime=mktime();
          $filename=$thistime.substr($file_name,strrpos($file_name, ". "));
          //文件名检查
        if($file_name   =   ' ')
            {
                echo   "文件名无效。 ";
                exit;
            }
      if(@move_uploaded_file($_files[ 'userfile '][ 'tmp_name '],$upload_dir.$filename))
          {
                echo   "上传成功。 ";
                echo   " <meta   http-equiv= "refresh "   content= "1;url=www.aimeige.com.cn.php "> ";
                exit;
          }else
            {
                echo   "上传失败。 ";
                echo   " <meta   http-equiv= "refresh "   content= "1;url=www.111cn.net.php "> ";
                exit;
            }
        echo   "end ";
    }
?>
 代码如下 复制代码
调用方法
 代码如下 复制代码
$upload_dir ='down.111cn.net'; $upload_url='www.111cn.net';
 代码如下 复制代码
 do_upload($upload_dir,$upload_url) ;
关于缓存技术不只在php有,很多系统都有,这是为了减轻服务吕压力与数据库压力来做的,本文章从php缓存技术入门到利用缓存实例来告诉你如何应用php来实例文件缓存描述。

先看这个缓存类

<?php

 代码如下 复制代码

class cache{
/*
class name: cache
description: control to cache data,$cache_out_time is a array to save cache date time out.
version: 1.0
author: 老农 cjjer
last modify:2006-2-26
author url: http://www.111cn.net*/
private $cache_dir;
private $expiretime=180;//缓存的时间是 60 秒
function __construct($cache_dirname){
    if(!@is_dir($cache_dirname)){
        if(!@mkdir($cache_dirname,0777)){
        $this->warn('缓存文件不存在而且不能创建,需要手动创建.');
        return false;
        }
    }
$this->cache_dir    =    $cache_dirname;
}
function __destruct(){
    echo 'cache class bye.';
}

function get_url() {
        if (!isset($_server['request_uri'])) {
                $url = $_server['request_uri'];
        }else{
                $url = $_server['script_name'];
                $url .= (!empty($_server['query_string'])) ? '?' . $_server['query_string'] : '';
        }

        return $url;
}

function warn($errorstring){
echo "<b><font color='red'>发生错误:<pre>".$errorstring."</pre></font></b>";
}

function cache_page($pageurl,$pagedata){
    if(!$fso=fopen($pageurl,'w')){
        $this->warns('无法打开缓存文件.');//trigger_error
        return false;
    }
    if(!flock($fso,lock_ex)){//lock_nb,排它型锁定
        $this->warns('无法锁定缓存文件.');//trigger_error
        return false;
    }
    if(!fwrite($fso,$pagedata)){//写入字节流,serialize写入其他格式
        $this->warns('无法写入缓存文件.');//trigger_error
        return false;
    }
    flock($fso,lock_un);//释放锁定
    fclose($fso);
    return true;
}

 

[!--infotagslink--]

相关文章

  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • MySQL性能监控软件Nagios的安装及配置教程

    这篇文章主要介绍了MySQL性能监控软件Nagios的安装及配置教程,这里以CentOS操作系统为环境进行演示,需要的朋友可以参考下...2015-12-14
  • php简单数据操作的实例

    最基础的对数据的增加删除修改操作实例,菜鸟们收了吧...2013-09-26
  • 解决Mybatis 大数据量的批量insert问题

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

    这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17
  • 详解如何清理redis集群的所有数据

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

    新版 Mysql 中加入了对 JSON Document 的支持,可以创建 JSON 类型的字段,并有一套函数支持对JSON的查询、修改等操作,下面就实际体验一下...2016-08-23
  • vue 获取到数据但却渲染不到页面上的解决方法

    这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
  • php把读取xml 文档并转换成json数据代码

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

    这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
  • 深入研究mysql中的varchar和limit(容易被忽略的知识)

    为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类型的问题。 ...2015-03-15
  • postgresql数据添加两个字段联合唯一的操作

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

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • mysql的3种分表方案

    一、先说一下为什么要分表:当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,有可能会死在那儿了。分表的目的就在于此,减小数据库的负担,缩短查询时间。根据个人经验,mysql执行一个sql的过程如下:1...2014-05-31
  • Vue生命周期activated之返回上一页不重新请求数据操作

    这篇文章主要介绍了Vue生命周期activated之返回上一页不重新请求数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-26
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • c# socket网络编程接收发送数据示例代码

    这篇文章主要介绍了c# socket网络编程,server端接收,client端发送数据,大家参考使用吧...2020-06-25
  • 用VirtualBox构建MySQL测试环境

    宿主机使用网线的时候,客户机在Bridged Adapter模式下,使用Atheros AR8131 PCI-E Gigabit Ethernet Controller上网没问题。 宿主机使用无线的时候,客户机在Bridged Adapter模式下,使用可选项里唯一一个WIFI选项,Microsoft Virtual Wifi Miniport Adapter也无法上网,故弃之。...2013-09-19
  • Centos5.5中安装Mysql5.5过程分享

    这几天在centos下装mysql,这里记录一下安装的过程,方便以后查阅Mysql5.5.37安装需要cmake,5.6版本开始都需要cmake来编译,5.5以后的版本应该也要装这个。安装cmake复制代码 代码如下: [root@local ~]# wget http://www.cm...2015-03-15
  • 解决vue watch数据的方法被调用了两次的问题

    这篇文章主要介绍了解决vue watch数据的方法被调用了两次的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-07