用PHP作为Shell脚本语言的使用

-------------------------------------------简单实例

首先要找到php执行文件

[root@kolvin centos]# whereis php
php: /usr/bin/php /etc/php.d /etc/php.ini /usr/lib/php /usr/share/man/man1/php.1.gz

 

创建test.php文件,代码如下:

#!/usr/bin/php -q
<?
print("Hello, world!\n");
?>

 

#chmod +x ./test.php
#/usr/bin/php test.php

 

 

 

 

 

-------------------------------------php作为shell核对用户使用空间问题

 

#!/usr/bin/php -q
<?php
class db {
    var $connection_id = "";
    var $pconnect = 0;
    var $shutdown_queries = array();
    var $queries = array();
    var $query_id = "";
    var $query_count = 0;
    var $record_row = array();
    var $failed = 0;
    var $halt = "";
    var $query_log = array();
    var $db_setting=array();
    function connect($db_config){
        if ($this->pconnect){
            $this->connection_id = mysql_pconnect($db_config["host"], $db_config["admin"], $db_config["database_pwd"]);
        }else{
            $this->connection_id = mysql_connect($db_config["host"], $db_config["admin"], $db_config["database_pwd"]);
        }
        if ( ! $this->connection_id ){
            $this->halt("Can not connect MySQL Server");
        }
        if ( ! @mysql_select_db($db_config["database"], $this->connection_id) ){
            $this->halt("Can not connect MySQL Database");
        }
        if ($db_config["charset"]) {
            @mysql_unbuffered_query("SET NAMES '".str_replace("-","",$db_config["charset"]."'"));
        }
        $this->db_setting=$db_config;
        return true;
    }

    function query($query_id, $query_type='mysql_query'){
        $this->query_id = $query_type($query_id, $this->connection_id);
        $this->queries[] = $query_id;
        if (! $this->query_id ) {
            $this->halt("error:\n$query_id");
        }
        $this->query_count++;
        return $this->query_id;
    }

    function query_unbuffered($sql=""){
        return $this->query($sql, 'mysql_unbuffered_query');
    }

    function fetch_array($sql = ""){
        if ($sql == "") $sql = $this->query_id;
        $this->record_row = @mysql_fetch_array($sql, MYSQL_ASSOC);
        return $this->record_row;
    }
    function shutdown_query($query_id = ""){
        $this->shutdown_queries[] = $query_id;
    }

    function affected_rows() {
        return @mysql_affected_rows($this->connection_id);
    }

    function num_rows($query_id="") {
        if ($query_id == "") $query_id = $this->query_id;
        return @mysql_num_rows($query_id);
    }

    function get_errno(){
        $this->errno = @mysql_errno($this->connection_id);
        return $this->errno;
    }

    function insert_id(){
        return @mysql_insert_id($this->connection_id);
    }

    function query_count() {
        return $this->query_count;
    }

    function free_result($query_id=""){
           if ($query_id == "") $query_id = $this->query_id;
        @mysql_free_result($query_id);
    }

    function close_db(){
        if ( $this->connection_id ) return @mysql_close( $this->connection_id );
    }

    function get_table_names(){
        $result = mysql_list_tables($this->db_setting["database"]);
        $num_tables = @mysql_numrows($result);
        for ($i = 0; $i < $num_tables; $i++) {
            $tables[] = mysql_tablename($result, $i);
        }
        mysql_free_result($result);
        return $tables;
       }

    function get_result_fields($query_id=""){
           if ($query_id == "") $query_id = $this->query_id;
        while ($field = mysql_fetch_field($query_id)) {
            $fields[] = $field;
        }
        return $fields;
       }

    function halt($the_error=""){
        $message = $the_error."<br/>\r\n";
        $message.= $this->get_errno() . "<br/>\r\n";
        $sql = "INSERT INTO `db_error`(pagename, errstr, timer) VALUES('".$_SERVER["PHP_SELF"]."', '".addslashes($message)."', ".time().")";
        @mysql_unbuffered_query($sql);
        if (DEBUG==true){
            echo "<html><head><title>MySQL database error</title>";
            echo "<style type=\"text/css\"><!--.error { font: 11px tahoma, verdana, arial, sans-serif, simsun; }--></style></head>\r\n";
            echo "<body>\r\n";
            echo "<blockquote>\r\n";
            echo "<textarea class=\"error\" rows=\"15\" cols=\"100\" wrap=\"on\" >" . htmlspecialchars($message) . "</textarea>\r\n";
            echo "</blockquote>\r\n</body></html>";
            exit;
        }
    }
    function __destruct(){
        $this->shutdown_queries = array();
        $this->close_db();
    }
    function sql_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){
        $sql = "SELECT ".$fields." FROM `".$tbname."` ".($where?" WHERE ".$where:"")." ORDER BY ".$orderby." ".$sort.($limit ? " limit ".$limit:"");
        return $sql;
    }
    function sql_insert($tbname, $row){
        foreach ($row as $key=>$value) {
            $sqlfield .= $key.",";
            $sqlvalue .= "'".$value."',";
        }
        return "INSERT INTO `".$tbname."`(".substr($sqlfield, 0, -1).") VALUES (".substr($sqlvalue, 0, -1).")";
    }
    function sql_update($tbname, $row, $where){
        foreach ($row as $key=>$value) {
            $sqlud .= $key."= '".$value."',";
        }
        return "UPDATE `".$tbname."` SET ".substr($sqlud, 0, -1)." WHERE ".$where;
    }
    function sql_delete($tbname, $where){
        return "DELETE FROM `".$tbname."` WHERE ".$where;
    }

    function row_insert($tbname, $row){
        $sql = $this->sql_insert($tbname, $row);
        return $this->query_unbuffered($sql);
    }

    function row_update($tbname, $row, $where){
        $sql = $this->sql_update($tbname, $row, $where);
        return $this->query_unbuffered($sql);
    }

    function row_delete($tbname, $where){
        $sql = $this->sql_delete($tbname, $where);
        return $this->query_unbuffered($sql);
    }

    function row_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){
        $sql = $this->sql_select($tbname, $where, $limit, $fields, $orderby, $sort);
        return $this->row_query($sql);
    }
    function return_string(){
        return $this->db_setting;
    }
    function row_select_one($tbname, $where, $fields="*", $orderby="id"){
        $sql = $this->sql_select($tbname, $where, 1, $fields, $orderby);
        return $this->row_query_one($sql);
    }
    function row_query($sql){
        $rs     = $this->query($sql);
        $rs_num = $this->num_rows($rs);
        $rows = array();
        for($i=0; $i<$rs_num; $i++){
            $rows[] = $this->fetch_array($rs);
        }
        $this->free_result($rs);
        return $rows;
    }
    function row_query_one($sql){
        $rs     = $this->query($sql);
        $row = $this->fetch_array($rs);
        $this->free_result($rs);
        return $row;
    }

    function row_count($tbname, $where=""){
        $sql = "SELECT count(id) as row_sum FROM `".$tbname."` ".($where?" WHERE ".$where:"");
        $row = $this->row_query_one($sql);
        return $row["row_sum"];
    }

    function get_version(){
        return mysql_get_server_info();
    }
}


$db_config=array(
'host'=>'localhost',               
'admin'=>'root',                   
'charset'=>'utf-8',                   
'database_pwd'=>'2009',       
'database'=>'***',       
);

$db=new db();
$db->connect($db_config);
$sql="select id,u_sn,username,space_size,space_used from member where space_used!='' order by id desc ";   
$result=$db->query($sql);
if($result)
{
    while($tmp_array=$db->fetch_array($result))
    {
        $sql2="select sum(size) as all_size from seeds where u_id = ".$tmp_array['id'];   
        $result2=$db->query($sql2);
        $tmp_array2=$db->fetch_array($result2);
        if($tmp_array['space_used']!=$tmp_array2['all_size'])
        {
            $sql="update member set space_used=".$tmp_array2['all_size']." where id=".$tmp_array['id'];
            $db->query($sql);
        }
    }
}else
{
        print("no member!");
}

?>

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值