用php生成php脚本

看到这篇文章,很有趣:超链接地址Generating PHP with Ruby

下面是个用php生成php简单的例子:

< ?php
// Some code
$code = ‘
<?php
echo “Hello World!”;
?>
‘;

// Write code to file
file_put_contents(‘generated.php’,$code); # PHP5 function!

// Include the generated code for execution
include ‘generated.php’;
?>

为什么不直接使用eval呢?我的理解是,这样只需要将生成的代码包含进来直接运行便是,省去了eval的解析时间,效率可能会更高.

单子模式(SINGLETON)的简单实现

参考了超链接地址DevNet超链接地址Chinaunix.net 的讨论,用以下代码简单实现单子模式(SINGLETON):

代码片段:


<?php 
/**
*  Class: SingletonPool
*  Store multiple singletons (of different classes, naturally).
*
*/
class Singleton {

    
/**
    * 返回唯一对象,可保存多个对象
    *
    * @param (string) $class - the class name
    * @return object
    * @access public
    * @static
    */
    
function &getInstance($class) {
        static
$ob = array();

        if( !isset(
$ob[$class]) ) {
            
$ob[$class] = & new $class;
        }

        return
$ob[$class];
    }

}
?>

Apache的AddDefaultCharset

看了这篇文章:超链接地址配置 Apache 强行指定浏览器编码选择 ,俺也有些感想:

apache 2的配置文件中,AddDefaultCharset缺省是ISO-8859-1,所以很多人刚开始用的时候会把它改成GB2312,这样就可以避免乱码问题.

我也推荐人家用这个解决办法在一段时间里,直到有一次看到一个UTF-8编码的页面,始终是乱码,无可救药的乱码,任你如何调整浏览器的设置.无奈之下我把AddDefaultCharset那行整个屏蔽了,突然发现所有UTF-8页面恢复了正常,真的好神奇耶!!

从此,我深深的认识到:在AddDefaultCharset被屏蔽的情况下,页面的编码是由<meta>标记决定的.

在psftp中进行批处理操作

在psftp(sftp的命令行客户端)中进行批处理操作:

1.首先建立文件->myscript.scr,内容如下:

代码片段:


cd /home/ftp/users/jeff
del jam-old.tar.gz
ren jam.tar.gz jam-old.tar.gz
put jam.tar.gz
chmod a+r jam.tar.gz
quit

2.然后你可以运行下面命令行:

psftp user@hostname -b myscript.scr

myscript.scr中所写的内容就会执行下去

3.有什么用呢?在windows下写个批处理.bat,配合editplus上传文件,爽翻你

附命令行帮助:

代码片段:


PuTTY Secure File Transfer (SFTP) client
Release 0.56
Usage: psftp [options] [user@]host
Options:
  -b file   use specified batchfile
  -bc       output batchfile commands
  -be       don't stop batchfile processing if errors
  -v        show verbose messages
  -load sessname  Load settings from saved session
  -l user   connect with specified username
  -P port   connect to specified port
  -pw passw login with specified password
  -1 -2     force use of particular SSH protocol version
  -C        enable compression
  -i key    private key file for authentication
  -batch    disable all interactive prompts
  -V        print version information

Eclipse无法启动

受网上某些文章诱惑,决定安装个Eclipse编辑器来玩玩.粉简单的事情,屡次失败.只好拍桌子骂Eclipse弱智并弃之.

但是google实在是无所不能,找到以下文章解决了问题:

http://blog.csdn.net/shaokun305/archive/2004/09/22/112627.aspx

原来是oracle客户端自带的jdk捣鬼,删掉后eclipse正常启动.

新闻组上看到的mysql cluster安装过程

如下:

 I have recently had success setting up a MySQL Cluster, so I
thought I’d share what I did with everyone since it was such a challenge
for me to do and the documentation for how to set it up is so poor.
 

[@more@] 

######################### My Setup ####################################

Here is my setup:

I have two machines, Machine A and Machine B, that have requests and
updates to the db balanced by a router.  There is one IP address that
clients use to access the db.  The router distributes 50% of the
requests to Machine A and the remaining 50% to Machine B.  Therefore,
the db on both Machine A and Machine B must be exactly the same at all
times.  To achieve this I used MySQL Cluster.

 

I am running Redhat Linux 9 on both machines, and the machines are
physically identical.

 

Machine A ip = 10.0.2.39

Machine B ip = 10.0.2.38

########################### Installing ################################ 

I did the following on both machines:

 

- I installed MySql-Max (Linux (x86, glibc-2.2, static, gcc) 4.1.7 

http://dev.mysql.com/get/Downloads/MySQL-4.1/mysql-max-4.1.7-pc-linux-i6
86.tar.gz/from/pick

 

- I extracted the tar file into the root folder, and installed the mysql
db by running the mysql_install_db script in the scripts directory.  (In
the mysql directory type-  ./scripts/mysql_install_db)

 

- I then created a config.ini file which I placed in the mysql
directory.  I also created a my.cnf file which I placed in the
mysql/data directory.

################### Config.ini #######################################

 [NDBD DEFAULT]

NoOfReplicas= 2

 

[MYSQLD DEFAULT]

[NDB_MGMD DEFAULT]

[TCP DEFAULT]

 

[NDB_MGMD]

HostName= 10.0.2.39      (10.0.2.38 on machine B)

 

[NDBD]

HostName= 10.0.2.38

DataDir= /root/mysql/data

 

[NDBD]

HostName= 10.0.2.39

DataDir= /root/mysql/data

 

[MYSQLD]

[MYSQLD]

[MYSQLD]

###################### My.cnf ########################## 

The my.cnf file on both machines is:

[mysqld]

ndbcluster

###################### Starting the cluster ############# 

To start the cluster I did the following:

(please note that MySQL is installed in the /root/mysql directory)

 

Machine A:

- opened a terminal.

- typed-   cd mysql

- typed-   ./bin/ndb_mgmd

       - starts the cluster manager

- typed-   ./bin/ndbd --initial

       - please note, only use the '--initial' parameter if it is the

        first time you are starting the node.  

- typed- ./bin/mysqld_safe --user=root &

       - starts mysql.  The terminal may stick here so use ctrl-c to get

         a new prompt.

- typed- ./bin/mysql

       - starts the mysql monitor.

- typed-  use test;

       - to use the test database.

 

Machine B:

- follow the same steps as Machine A:

###################### Testing the cluster ############# 

On Machine A:

- at the mysql monitor prompt

- type-  use test;

      - to use the test database.

- type-  create table cluster_test (i int) engine=ndbcluster;

      - this should create a clustered table called cluster_test.

 

On Machine B:

- at the mysql monitor prompt

- type-  use test;

- type-  show tables;

      -  this should show the cluster_test table created on Machine A.

 

Inserting

Subsequent inserts etc. can be done at the mysql monitor prompt.

Example:

On Machine A:

-        type- insert into cluster_test values(314159);

 

On Machine B:

-        type- select * from cluster_test;

    - should display the value 314159.

##########################################################

ERROR 1114: The table ‘big_table_a’ is full

在新闻组上看到的关于mysql cluster的问题:

[@more@]

At us is 1 MGM node and 4 DB nodes! 
Each of 5 computers have 1Gb operative memory! 
We have created 3 SQL a file (everyone on 900Mb) 
each of which has consisted of 10 000 000 lines 

insert into big_table_a set a1='213123', a2='123123', a3='213124', 
a4='234234';
insert into big_table_a set a1='213123', a2='978433', a3='234234', 
a4='2535623';
............................

Through API node we have started this SQL a script with the help of a 
command 

source data1.sql;
source data2.sql;
source data3.sql;

When in a database already has entered the name more than 
14 000 000 lines MySQL Cluster has given out the following mistake: 

ERROR 1114: The table 'big_table_a' is full

回复:

If your big_table has a primary key it will be using up your index_memory in
a short time.
Also, you may want to disconnect/reconnect while inserting all the data.
To understand what your memory is used for you should read:
http://dev.mysql.com/doc/mysql/en/MySQL_Cluster_DB_Definition.html
(datamemory and index memory part). 

Generally 1gig of memory is not a lot. I have the same setup but with 2gigs
of ram in each machine and I have experienced the same problems.

What datatypes are the columns in your table?
You can calculate the amount of memory that your table takes up and find out
if you have enough space.