当前位置: 编程技术>php
本页文章导读:
▪java EJB 加密与解密原理的一个例子
加密与解密原理的一个例子 package lockunlock; import Java.awt.*; import java.awt.event.*; import java.Applet.*; import javax.Swing.*; import java.util.*; public class LockUnlock extends JApplet { private boolean.........
▪apache rewrite_module模块使用教程
把 [url]http://wwww.aaaaaaaaa.com/bbb.php?id=888[/url] 的地址形式改为 [url]http://wwww.aaaaaaaaa.com/888.htm[/url] 或者 [url]http://wwww.aaaaaaaaa.com/yourname/888.htm[/url] 当然可以按照你的要求随便变. APACHE的 MOD_rewrite.........
▪支持php4、php5的mysql数据库操作类
前端一直使用PHP5,的确使用起来特别的爽,现在为了能在俺的虚拟主机上跑,不得不改成PHP4的。这几个库类我以前发在PHPCHIAN,地址是http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight=。.........
[1]java EJB 加密与解密原理的一个例子
来源: 互联网 发布时间: 2013-11-30
加密与解密原理的一个例子
package lockunlock;
import Java.awt.*;
import java.awt.event.*;
import java.Applet.*;
import javax.Swing.*;
import java.util.*;
public class LockUnlock extends JApplet {
private boolean isStandalone = false;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public LockUnlock() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jLabel1.setText("String");
jLabel1.setBounds(new Rectangle(35, 36, 57, 21));
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
jLabel2.setText("String length");
jLabel2.setBounds(new Rectangle(29, 73, 69, 22));
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(108, 40, 166, 17));
jTextField2.setText("");
jTextField2.setBounds(new Rectangle(107, 72, 56, 21));
jButton1.setBounds(new Rectangle(30, 236, 137, 27));
jButton1.setText("Exercise 3");
jButton1.addActionListener(new LockUnlock_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(218, 237, 131, 27));
jButton2.setText("Exercise 4");
jButton2.addActionListener(new LockUnlock_jButton2_actionAdapter(this));
jTextField3.setText("");
jTextField3.setBounds(new Rectangle(106, 105, 58, 21));
jLabel3.setText("MoShu");
jLabel3.setBounds(new Rectangle(36, 106, 86, 18));
contentPane.add(jLabel1, null);
contentPane.add(jButton2, null);
contentPane.add(jButton1, null);
contentPane.add(jLabel3, null);
contentPane.add(jTextField2, null);
contentPane.add(jLabel2, null);
contentPane.add(jTextField3, null);
contentPane.add(jTextField1, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
LockUnlock applet = new LockUnlock();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
//Declare DataMember
int index;
//-----------------------------------------------------
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField3 = new JTextField();
JLabel jLabel3 = new JLabel();
//----------------------N!------------------------------
public int function(int N){
if(N==1)
return 1;
else{
return N*function(N-1);
/*不是RETURN function(N-1);
而是 N*function(N-1);*/
}
}
//-----------用递归法求一个串的全排列-----------------------
public void Arrange(String prefix,String suffix,int[] Array){
String newPrefix,newSuffix;
int numOfChars =suffix.length();
if(numOfChars==1){
Array[index]=Integer.parseInt(prefix+suffix);
index++;
}
else{
for(int i=1; i<=numOfChars;i++){
newSuffix=suffix.substring(1,numOfChars);
newPrefix=prefix+suffix.charAt(0);
Arrange(newPrefix,newSuffix,Array);
suffix=newSuffix+suffix.charAt(0);
}
}
}
//----------Arrange From the Min to the Max------------------
/*public void RankForArrange(int[] Array){
int bottom=Array.length-1 ;
int temp;
for(int i=bottom;i>0;i--){
for(int j=0;j<i;j++){
if(Array[j]>Array[j+1]){
temp =Array[j];
Array[j] =Array[j+1];
Array[j+1]=temp;
}
}
}
}
*/
//-------------------Find the aim number----------------------
public int FindAim(int aim,int[] Array){
boolean isFound=false;
int location=0;
int length=Array.length ;
for(int i=0;i<length;i++){
if(Array[i]==aim){
location=i;
isFound =true;
}
}
if(isFound)
return location;
else
System.out.println("Not Found");
return location;
/*在if里return 不行吗?*/
}
//------------------Creat String-------------------------------
public String CreatString(int length){
StringBuffer BufString=new StringBuffer();
for(int i=1;i<=length;i++){
BufString.append(i) ;
}
return BufString.toString();
}
//-----------OutPut Result--------------------
public void OutPutResult1(){
index = 0; //clear to 0
String AimString, prefix;
AimString = jTextField1.getText();
int Length = AimString.length();
String strLength = String.valueOf(Length);
int Aim = Integer.parseInt(AimString);
/* 方法.parseInt才是转换为int类型
而不是.getInteger*/
int[] EachArrange = new int[this.function(Length)];
jTextField2.setText(strLength);
prefix = ""; //Make an empty String
if (AimString.length() > 2 &&
AimString.length() < 9 && AimString != "") {
Arrange(prefix, AimString, EachArrange);
//RankForArrange(EachArrange);
Arrays.sort(EachArrange);
String result = String.valueOf(FindAim(Aim, EachArrange));
jTextField3.setText(result);
}
else {
System.out.println("Your String is too short");
}
}
//----------Out put result 2---------------------
public void OutPutRestlt2(){
index=0;//Let index come back to 0
String strLength, strMoShu,
AimString, prefix,suffix;
int Length, MoShu,limit;
strLength = jTextField2.getText();
strMoShu = jTextField3.getText();
Length = Integer.parseInt(strLength);
MoShu = Integer.parseInt(strMoShu);
limit = function(Length);
int[] EachArrange = new int[this.function(Length)];
if (Length > 2&&Length<9&&
strLength!=""&&strMoShu!=""
&&MoShu<limit) {
prefix = "";
suffix =CreatString(Length);
Arrange(prefix, suffix, EachArrange);
Arrays.sort(EachArrange);
String strResult=String.valueOf(EachArrange[MoShu]);
jTextField1.setText(strResult);
}
else
System.out.println("Input Ouf MoShu, Try again") ;
}
void jButton1_actionPerformed(ActionEvent e) {
this.OutPutResult1();
}
void jButton2_actionPerformed(ActionEvent e) {
this.OutPutRestlt2();
}
//-----------------------------------------------------------
}
class LockUnlock_jButton1_actionAdapter implements java.awt.event.ActionListener {
LockUnlock adaptee;
LockUnlock_jButton1_actionAdapter(LockUnlock adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class LockUnlock_jButton2_actionAdapter implements java.awt.event.ActionListener {
LockUnlock adaptee;
LockUnlock_jButton2_actionAdapter(LockUnlock adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
package lockunlock;
import Java.awt.*;
import java.awt.event.*;
import java.Applet.*;
import javax.Swing.*;
import java.util.*;
public class LockUnlock extends JApplet {
private boolean isStandalone = false;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public LockUnlock() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jLabel1.setText("String");
jLabel1.setBounds(new Rectangle(35, 36, 57, 21));
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
jLabel2.setText("String length");
jLabel2.setBounds(new Rectangle(29, 73, 69, 22));
jTextField1.setText("");
jTextField1.setBounds(new Rectangle(108, 40, 166, 17));
jTextField2.setText("");
jTextField2.setBounds(new Rectangle(107, 72, 56, 21));
jButton1.setBounds(new Rectangle(30, 236, 137, 27));
jButton1.setText("Exercise 3");
jButton1.addActionListener(new LockUnlock_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(218, 237, 131, 27));
jButton2.setText("Exercise 4");
jButton2.addActionListener(new LockUnlock_jButton2_actionAdapter(this));
jTextField3.setText("");
jTextField3.setBounds(new Rectangle(106, 105, 58, 21));
jLabel3.setText("MoShu");
jLabel3.setBounds(new Rectangle(36, 106, 86, 18));
contentPane.add(jLabel1, null);
contentPane.add(jButton2, null);
contentPane.add(jButton1, null);
contentPane.add(jLabel3, null);
contentPane.add(jTextField2, null);
contentPane.add(jLabel2, null);
contentPane.add(jTextField3, null);
contentPane.add(jTextField1, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//Main method
public static void main(String[] args) {
LockUnlock applet = new LockUnlock();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
//Declare DataMember
int index;
//-----------------------------------------------------
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField2 = new JTextField();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JTextField jTextField3 = new JTextField();
JLabel jLabel3 = new JLabel();
//----------------------N!------------------------------
public int function(int N){
if(N==1)
return 1;
else{
return N*function(N-1);
/*不是RETURN function(N-1);
而是 N*function(N-1);*/
}
}
//-----------用递归法求一个串的全排列-----------------------
public void Arrange(String prefix,String suffix,int[] Array){
String newPrefix,newSuffix;
int numOfChars =suffix.length();
if(numOfChars==1){
Array[index]=Integer.parseInt(prefix+suffix);
index++;
}
else{
for(int i=1; i<=numOfChars;i++){
newSuffix=suffix.substring(1,numOfChars);
newPrefix=prefix+suffix.charAt(0);
Arrange(newPrefix,newSuffix,Array);
suffix=newSuffix+suffix.charAt(0);
}
}
}
//----------Arrange From the Min to the Max------------------
/*public void RankForArrange(int[] Array){
int bottom=Array.length-1 ;
int temp;
for(int i=bottom;i>0;i--){
for(int j=0;j<i;j++){
if(Array[j]>Array[j+1]){
temp =Array[j];
Array[j] =Array[j+1];
Array[j+1]=temp;
}
}
}
}
*/
//-------------------Find the aim number----------------------
public int FindAim(int aim,int[] Array){
boolean isFound=false;
int location=0;
int length=Array.length ;
for(int i=0;i<length;i++){
if(Array[i]==aim){
location=i;
isFound =true;
}
}
if(isFound)
return location;
else
System.out.println("Not Found");
return location;
/*在if里return 不行吗?*/
}
//------------------Creat String-------------------------------
public String CreatString(int length){
StringBuffer BufString=new StringBuffer();
for(int i=1;i<=length;i++){
BufString.append(i) ;
}
return BufString.toString();
}
//-----------OutPut Result--------------------
public void OutPutResult1(){
index = 0; //clear to 0
String AimString, prefix;
AimString = jTextField1.getText();
int Length = AimString.length();
String strLength = String.valueOf(Length);
int Aim = Integer.parseInt(AimString);
/* 方法.parseInt才是转换为int类型
而不是.getInteger*/
int[] EachArrange = new int[this.function(Length)];
jTextField2.setText(strLength);
prefix = ""; //Make an empty String
if (AimString.length() > 2 &&
AimString.length() < 9 && AimString != "") {
Arrange(prefix, AimString, EachArrange);
//RankForArrange(EachArrange);
Arrays.sort(EachArrange);
String result = String.valueOf(FindAim(Aim, EachArrange));
jTextField3.setText(result);
}
else {
System.out.println("Your String is too short");
}
}
//----------Out put result 2---------------------
public void OutPutRestlt2(){
index=0;//Let index come back to 0
String strLength, strMoShu,
AimString, prefix,suffix;
int Length, MoShu,limit;
strLength = jTextField2.getText();
strMoShu = jTextField3.getText();
Length = Integer.parseInt(strLength);
MoShu = Integer.parseInt(strMoShu);
limit = function(Length);
int[] EachArrange = new int[this.function(Length)];
if (Length > 2&&Length<9&&
strLength!=""&&strMoShu!=""
&&MoShu<limit) {
prefix = "";
suffix =CreatString(Length);
Arrange(prefix, suffix, EachArrange);
Arrays.sort(EachArrange);
String strResult=String.valueOf(EachArrange[MoShu]);
jTextField1.setText(strResult);
}
else
System.out.println("Input Ouf MoShu, Try again") ;
}
void jButton1_actionPerformed(ActionEvent e) {
this.OutPutResult1();
}
void jButton2_actionPerformed(ActionEvent e) {
this.OutPutRestlt2();
}
//-----------------------------------------------------------
}
class LockUnlock_jButton1_actionAdapter implements java.awt.event.ActionListener {
LockUnlock adaptee;
LockUnlock_jButton1_actionAdapter(LockUnlock adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class LockUnlock_jButton2_actionAdapter implements java.awt.event.ActionListener {
LockUnlock adaptee;
LockUnlock_jButton2_actionAdapter(LockUnlock adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
[2]apache rewrite_module模块使用教程
来源: 互联网 发布时间: 2013-11-30
把
[url]http://wwww.aaaaaaaaa.com/bbb.php?id=888[/url]
的地址形式改为
[url]http://wwww.aaaaaaaaa.com/888.htm[/url]
或者
[url]http://wwww.aaaaaaaaa.com/yourname/888.htm[/url]
当然可以按照你的要求随便变.
APACHE的 MOD_rewrite模块.
大家可以看一个演示的一个 PHP学习论坛
[url]http://www.phpx.com/happy/[/url]
这个论坛的版面和帖子,就是运用了这个技术,地址静态化.但是是假的.
这个技术哪里好?
可以让baidu, google等收入你的站点所有页面.
收入地址就是你的假静态地址.当然别人看不出你是假的.而且这个技术隐藏了你背后执行的程序.
你可以把
/soft/1234.html 重写传递给 soft.php?id=1234
当然你改一下名 换成 softxfewafew.php?id=1234
表面还是 soft/1234.html但是你 APACHE内部执行了你重写的文件.
从根本上可以防止别人从程序本身入侵.
下面我写怎么样重写.分为 WINDOWS和LIUNX2种
都是操作
APACHE安装文件夹内的 CONF文件夹里面的httpd.conf
打开以后,找到
#LoadModule rewrite_module "modules/mod_rewrite.so"
把#去掉.
然后找到虚拟主机配置
在虚拟主机中加入
RewriteEngine On
RewriteRule ^/soft/([0-9]+).html$ /soft.php?id=$1
//解释
//WWW.玉米.COM/SOFT/1234.HTML
//重写为
//WWW.玉米.COM/soft.php?id=1234
//这里ID是可以变的 你给它 1 就是传递1
RewriteRule ^/([0-9]+).html$ /soft.php?id=$1
//解释
//WWW.玉米.COM/1234.HTML
//重写为
//WWW.玉米.COM/soft.php?id=1234
RewriteRule ^/([0-9]+)_([0-9]+).html$ /soft.php?id=$1&catid=$2
//解释
//WWW.玉米.COM/1234_2222.HTML
//重写为
//WWW.玉米.COM/soft.php?id=1234&catid=2222
当然随便你怎么换!
这就是 WIN下的.
LIUNX下是一样的 但是要加
<IfModule mod_rewrite.c>开始
</IfModule>结束
重写都加在虚拟主机设置中.
如果没有虚拟主机,那加在最后!
[url]http://wwww.aaaaaaaaa.com/bbb.php?id=888[/url]
的地址形式改为
[url]http://wwww.aaaaaaaaa.com/888.htm[/url]
或者
[url]http://wwww.aaaaaaaaa.com/yourname/888.htm[/url]
当然可以按照你的要求随便变.
APACHE的 MOD_rewrite模块.
大家可以看一个演示的一个 PHP学习论坛
[url]http://www.phpx.com/happy/[/url]
这个论坛的版面和帖子,就是运用了这个技术,地址静态化.但是是假的.
这个技术哪里好?
可以让baidu, google等收入你的站点所有页面.
收入地址就是你的假静态地址.当然别人看不出你是假的.而且这个技术隐藏了你背后执行的程序.
你可以把
/soft/1234.html 重写传递给 soft.php?id=1234
当然你改一下名 换成 softxfewafew.php?id=1234
表面还是 soft/1234.html但是你 APACHE内部执行了你重写的文件.
从根本上可以防止别人从程序本身入侵.
下面我写怎么样重写.分为 WINDOWS和LIUNX2种
都是操作
APACHE安装文件夹内的 CONF文件夹里面的httpd.conf
打开以后,找到
#LoadModule rewrite_module "modules/mod_rewrite.so"
把#去掉.
然后找到虚拟主机配置
在虚拟主机中加入
RewriteEngine On
RewriteRule ^/soft/([0-9]+).html$ /soft.php?id=$1
//解释
//WWW.玉米.COM/SOFT/1234.HTML
//重写为
//WWW.玉米.COM/soft.php?id=1234
//这里ID是可以变的 你给它 1 就是传递1
RewriteRule ^/([0-9]+).html$ /soft.php?id=$1
//解释
//WWW.玉米.COM/1234.HTML
//重写为
//WWW.玉米.COM/soft.php?id=1234
RewriteRule ^/([0-9]+)_([0-9]+).html$ /soft.php?id=$1&catid=$2
//解释
//WWW.玉米.COM/1234_2222.HTML
//重写为
//WWW.玉米.COM/soft.php?id=1234&catid=2222
当然随便你怎么换!
这就是 WIN下的.
LIUNX下是一样的 但是要加
<IfModule mod_rewrite.c>开始
</IfModule>结束
重写都加在虚拟主机设置中.
如果没有虚拟主机,那加在最后!
[3]支持php4、php5的mysql数据库操作类
来源: 互联网 发布时间: 2013-11-30
前端一直使用PHP5,的确使用起来特别的爽,现在为了能在俺的虚拟主机上跑,不得不改成PHP4的。这几个库类我以前发在PHPCHIAN,地址是http://www.phpchina.com/bbs/viewthread.php?tid=5687&highlight=。(前几天在网上搜索了下,发现很多转载我的这几篇文章都没有说明出处,而且把我的版权都删除了,气晕了。)
昨天改写了数据库操作类,恰好在我简化zend Framework也能用到。
代码如下:
<?php
/**
* filename: DB_Mysql.class.php
* @package:phpbean
* @author :feifengxlq<[email]feifengxlq@gmail.com[/email]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.2
* create:2006-5-30
* modify:2006-10-19 by feifengxlq
* description:the interface of mysql.
*
* example:
* ////////////Select action (First mode)//////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->query("select * from test");
for($i=0;$i<$mysql->num_rows($rs);$i++)
$record[$i]=$mysql->seek($i);
print_r($record);
$mysql->close();
* ////////////Select action (Second mode)//////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->execute("select * from test");
print_r($rs);
$mysql->close();
* /////////////insert action////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$mysql->query("insert into test(username) values('test from my DB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
*/
class mysql{
/* private: connection parameters */
var $host="localhost";
var $database="";
var $user="root";
var $password="";
/* private: configuration parameters */
var $pconnect=false;
var $debug=false;
/* private: result array and current row number */
var $link_id=0;
var $query_id=0;
var $record=array();
/**
* construct
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
*/
function __construct($host="localhost",$user="root",$password="",$database="")
{
$this->set("host",$host);
$this->set("user",$user);
$this->set("password",$password);
$this->set("database",$database);
$this->connect();
}
/**
* set the value for the param of this class
*
* @param string $var
* @param string $value
*/
function set($var,$value)
{
$this->$var=$value;
}
/**
* connect to a mysql server,and choose the database.
*
* @param string $database
* @param string $host
* @param string $user
* @param string $password
* @return link_id
*/
function connect($database="",$host="",$user="",$password="")
{
if(!empty($database))$this->set("database",$database);
if(!empty($host))$this->set("host",$host);
if(!empty($user))$this->set("user",$user);
if(!empty($password))$this->set("password",$password);
if($this->link_id==0)
{
if($this->pconnect)
$this->link_id=@mysql_pconnect($this->host,$this->user,$this->password);
else
$this->link_id=@mysql_connect($this->host,$this->user,$this->password);
if(!$this->link_id)
die("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
if(!@mysql_select_db($this->database,$this->link_id))
die("Mysql Select database Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
}
return $this->link_id;
}
/**
* query a sql into the database
*
* @param string $strsql
* @return query_id
*/
function query($strsql="")
{
if(empty($strsql)) die("Mysql Error:".__FUNCTION__."() strsql is empty!");
if($this->link_id==0) $this->connect();
if($this->debug) printf("Debug query sql:%s",$strsql);
$this->query_id=@mysql_query($strsql,$this->link_id);
if(!$this->query_id) die("Mysql query fail,Invalid sql:".$strsql.".");
return $this->query_id;
}
/**
* query a sql into the database,while it is differernt from the query() method,
* this method will return a record(array);
*
* @param string $strsql
* @param string $style
* @return $record is a array()
*/
function Execute($strsql,$)
{
$this->query($strsql);
if(!empty($this->record))$this->record=array();
$i=0;
if($array"){
while ($temp=@mysql_fetch_array($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}else{
while ($temp=@mysql_fetch_object($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}
unset($i);
unset($temp);
return $this->record;
}
/**
* seek,but not equal to mysql_data_seek. this methord will return a list.
*
* @param int $pos
* @param string $style
* @return record
*/
function seek($pos=0,$)
{
if(!@mysql_data_seek($this->query_id,$pos))
die("Error in".__FUNCTION__."():can not seek to row ".$pos."!");
$result=@($array")?mysql_fetch_array($this->query_id):mysql_fetch_object($this->query_id);
if(!$result) die("Error in ".__FUNCTION__."():can not fetch data!");
return $result;
}
/**
* free the result of query
*
*/
function free()
{
if(($this->query_id)&($this->query_id!=0))@mysql_free_result($this->query_id);
}
/**
* evaluate the result (size, width)
*
* @return num
*/
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);
}
function insert_id()
{
return @mysql_insert_id($this->link_id);
}
function close()
{
$this->free();
if($this->link_id!=0)@mysql_close($this->link_id);
if(mysql_errno()!=0) die("Mysql Error:".mysql_errno().":".mysql_error());
}
function select($strsql,$number,$offset)
{
if(empty($number)){
return $this->Execute($strsql);
}else{
return $this->Execute($strsql.' limit '.$offset.','.$number);
}
}
function __destruct()
{
$this->close();
$this->set("user","");
$this->set("host","");
$this->set("password","");
$this->set("database","");
}
}
?>
在此基础上,我顺便封装SIDU(select,insert,update,delete)四种基本操作,作为简化的zend Framework的module。代码如下(这个没写注释了,懒的写。。):
<?
class module{
var $mysql;
var $tbname;
var $debug=false;
function __construct($tbname){
if(!is_string($tbname))die('Module need a args of tablename');
$this->tbname=$tbname;
$this->mysql=phpbean::registry('db');
}
function _setDebug($debug=true){
$this->debug=$debug;
}
function add($row){
if(!is_array($row))die('module error:row should be an array');
$strsql='insert into `'.$this->tbname.'`';
$keys='';
$values='';
foreach($row as $key=>$value){
$keys.='`'.$key.'`,';
$values.='\''.$value.'\'';
}
$keys=rtrim($keys,',');
$values=rtrim($values,',');
$strsql.=' ('.$keys.') values ('.$values.')';
if($this->debug)echo '<hr>'.$strsql.'<hr>';
$this->mysql->query($strsql);
return $this->mysql->insert_id();
}
function query($strsql){
return $this->mysql->Execute($strsql);
}
function count($where=''){
$strsql='select count(*) as num from `'.$this->tbname.'` ';
if(!empty($where))$strsql.=$where;
$rs=$this->mysql->Execute($strsql);
return $rs[0]['num'];
}
function select($where=''){
$strsql='select * from `'.$this->tbname.'` ';
if(!empty($where))$strsql.=$where;
return $this->mysql->Execute($strsql);
}
function delete($where=''){
if(empty($where))die('Error:the delete method need a condition!');
return $this->mysql->query('delete from `'.$this->tbname.'` '.$where);
}
function update($set,$where){
if(empty($where))die('Error:the update method need a condition!');
if(!is_array($set))die('Error:Set must be an array!');
$strsql='update `'.$this->tbname.'` ';
//get a string of set
$strsql.='set ';
foreach($set as $key=>$value){
$strsql.='`'.$key.'`=\''.$value.'\',';
}
$strsql=rtrim($strsql,',');
return $this->mysql->query($strsql.' '.$where);
}
function detail($where){
if(empty($where))die('Error:where should not empty!');
$rs=$this->mysql->query('select * from `'.$this->tbname.'` '.$where);
return $this->mysql->seek(0);
}
}
?>
昨天改写了数据库操作类,恰好在我简化zend Framework也能用到。
代码如下:
<?php
/**
* filename: DB_Mysql.class.php
* @package:phpbean
* @author :feifengxlq<[email]feifengxlq@gmail.com[/email]>
* @copyright :Copyright 2006 feifengxlq
* @license:version 1.2
* create:2006-5-30
* modify:2006-10-19 by feifengxlq
* description:the interface of mysql.
*
* example:
* ////////////Select action (First mode)//////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->query("select * from test");
for($i=0;$i<$mysql->num_rows($rs);$i++)
$record[$i]=$mysql->seek($i);
print_r($record);
$mysql->close();
* ////////////Select action (Second mode)//////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$rs=$mysql->execute("select * from test");
print_r($rs);
$mysql->close();
* /////////////insert action////////////////////////////
$mysql=new DB_Mysql("localhost","root","root","root");
$mysql->query("insert into test(username) values('test from my DB_mysql')");
printf("%s",$mysql->insert_id());
$mysql->close();
*/
class mysql{
/* private: connection parameters */
var $host="localhost";
var $database="";
var $user="root";
var $password="";
/* private: configuration parameters */
var $pconnect=false;
var $debug=false;
/* private: result array and current row number */
var $link_id=0;
var $query_id=0;
var $record=array();
/**
* construct
*
* @param string $host
* @param string $user
* @param string $password
* @param string $database
*/
function __construct($host="localhost",$user="root",$password="",$database="")
{
$this->set("host",$host);
$this->set("user",$user);
$this->set("password",$password);
$this->set("database",$database);
$this->connect();
}
/**
* set the value for the param of this class
*
* @param string $var
* @param string $value
*/
function set($var,$value)
{
$this->$var=$value;
}
/**
* connect to a mysql server,and choose the database.
*
* @param string $database
* @param string $host
* @param string $user
* @param string $password
* @return link_id
*/
function connect($database="",$host="",$user="",$password="")
{
if(!empty($database))$this->set("database",$database);
if(!empty($host))$this->set("host",$host);
if(!empty($user))$this->set("user",$user);
if(!empty($password))$this->set("password",$password);
if($this->link_id==0)
{
if($this->pconnect)
$this->link_id=@mysql_pconnect($this->host,$this->user,$this->password);
else
$this->link_id=@mysql_connect($this->host,$this->user,$this->password);
if(!$this->link_id)
die("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
if(!@mysql_select_db($this->database,$this->link_id))
die("Mysql Select database Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
}
return $this->link_id;
}
/**
* query a sql into the database
*
* @param string $strsql
* @return query_id
*/
function query($strsql="")
{
if(empty($strsql)) die("Mysql Error:".__FUNCTION__."() strsql is empty!");
if($this->link_id==0) $this->connect();
if($this->debug) printf("Debug query sql:%s",$strsql);
$this->query_id=@mysql_query($strsql,$this->link_id);
if(!$this->query_id) die("Mysql query fail,Invalid sql:".$strsql.".");
return $this->query_id;
}
/**
* query a sql into the database,while it is differernt from the query() method,
* this method will return a record(array);
*
* @param string $strsql
* @param string $style
* @return $record is a array()
*/
function Execute($strsql,$)
{
$this->query($strsql);
if(!empty($this->record))$this->record=array();
$i=0;
if($array"){
while ($temp=@mysql_fetch_array($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}else{
while ($temp=@mysql_fetch_object($this->query_id)) {
$this->record[$i]=$temp;
$i++;
}
}
unset($i);
unset($temp);
return $this->record;
}
/**
* seek,but not equal to mysql_data_seek. this methord will return a list.
*
* @param int $pos
* @param string $style
* @return record
*/
function seek($pos=0,$)
{
if(!@mysql_data_seek($this->query_id,$pos))
die("Error in".__FUNCTION__."():can not seek to row ".$pos."!");
$result=@($array")?mysql_fetch_array($this->query_id):mysql_fetch_object($this->query_id);
if(!$result) die("Error in ".__FUNCTION__."():can not fetch data!");
return $result;
}
/**
* free the result of query
*
*/
function free()
{
if(($this->query_id)&($this->query_id!=0))@mysql_free_result($this->query_id);
}
/**
* evaluate the result (size, width)
*
* @return num
*/
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);
}
function insert_id()
{
return @mysql_insert_id($this->link_id);
}
function close()
{
$this->free();
if($this->link_id!=0)@mysql_close($this->link_id);
if(mysql_errno()!=0) die("Mysql Error:".mysql_errno().":".mysql_error());
}
function select($strsql,$number,$offset)
{
if(empty($number)){
return $this->Execute($strsql);
}else{
return $this->Execute($strsql.' limit '.$offset.','.$number);
}
}
function __destruct()
{
$this->close();
$this->set("user","");
$this->set("host","");
$this->set("password","");
$this->set("database","");
}
}
?>
在此基础上,我顺便封装SIDU(select,insert,update,delete)四种基本操作,作为简化的zend Framework的module。代码如下(这个没写注释了,懒的写。。):
<?
class module{
var $mysql;
var $tbname;
var $debug=false;
function __construct($tbname){
if(!is_string($tbname))die('Module need a args of tablename');
$this->tbname=$tbname;
$this->mysql=phpbean::registry('db');
}
function _setDebug($debug=true){
$this->debug=$debug;
}
function add($row){
if(!is_array($row))die('module error:row should be an array');
$strsql='insert into `'.$this->tbname.'`';
$keys='';
$values='';
foreach($row as $key=>$value){
$keys.='`'.$key.'`,';
$values.='\''.$value.'\'';
}
$keys=rtrim($keys,',');
$values=rtrim($values,',');
$strsql.=' ('.$keys.') values ('.$values.')';
if($this->debug)echo '<hr>'.$strsql.'<hr>';
$this->mysql->query($strsql);
return $this->mysql->insert_id();
}
function query($strsql){
return $this->mysql->Execute($strsql);
}
function count($where=''){
$strsql='select count(*) as num from `'.$this->tbname.'` ';
if(!empty($where))$strsql.=$where;
$rs=$this->mysql->Execute($strsql);
return $rs[0]['num'];
}
function select($where=''){
$strsql='select * from `'.$this->tbname.'` ';
if(!empty($where))$strsql.=$where;
return $this->mysql->Execute($strsql);
}
function delete($where=''){
if(empty($where))die('Error:the delete method need a condition!');
return $this->mysql->query('delete from `'.$this->tbname.'` '.$where);
}
function update($set,$where){
if(empty($where))die('Error:the update method need a condition!');
if(!is_array($set))die('Error:Set must be an array!');
$strsql='update `'.$this->tbname.'` ';
//get a string of set
$strsql.='set ';
foreach($set as $key=>$value){
$strsql.='`'.$key.'`=\''.$value.'\',';
}
$strsql=rtrim($strsql,',');
return $this->mysql->query($strsql.' '.$where);
}
function detail($where){
if(empty($where))die('Error:where should not empty!');
$rs=$this->mysql->query('select * from `'.$this->tbname.'` '.$where);
return $this->mysql->seek(0);
}
}
?>
最新技术文章: