用Thinkphp写的简单程序在命令行执行(非 Thinkphp 下的cli模式 而是php下的命令模式)
来源:互联网 发布:淘宝运动壹号高仿 编辑:程序博客网 时间:2024/06/10 15:55
本来是打算用Thinkphp下的cli命令模式 运行程序的,但不知道哪里出错了,没法跑起来 于是就有了下篇代码。
首先 用Thinkphp写个简单的程序,保证页面上访问输出正常。
再在命令行 输入 php index.php --start=1 --end=9 (--start=1 --end=9 参数的传递 这里的--代表标识符,)
下面是部分代码:
tp/Conf/config.php 配置数据库相关信息
<?php
return array(
//'配置项'=>'配置值'
// 添加数据库配置信息
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'middleData', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => 'root', // 密码
'DB_PORT' => 3306, // 端口
'DB_PREFIX' => '', // 数据库表前缀
);
?>
tp/index.php 入口文件的代码。
<?php
define('APP_DEBUG',TRUE);
//define('MODE_NAME','cli');
include './ThinkPHP/ThinkPHP.php';
//App::run();
?>
tp/Lib/Action/Index.Action.class.php 代码
<?php
class IndexAction extends Action {
public function index(){
$Enterprise_raw = M('Enterprise_raw'); // 实例化Data数据模型
$argv=$_SERVER["argv"];
//print_r($argc);
$argc=$this->parseArgs($argv);
$start=$argc[start];
$end=$argc[end];
$rawRecs=$Enterprise_raw->limit($start,$end)->select();
$Enterprise = M('Enterprise'); // 实例化Data数据模型
//echo $Enterprise_raw->getLastSql();
$count = 0;
echo "程序开始时间:".date("Y-m-d H:m:s",time())."<br/>";
foreach($rawRecs as $rawRec){
$record=$Enterprise->where("name='".$rawRec['enterprise_name']."'")->select();
switch ($rawRec['status']) {
case 0:
if($record){
$this->updataRec($rawRec);
//echo '<br/>'.'update rec successuflly with : ' . $rawRec['enterprise_name'];
}else{
$this->insertRec($rawRec);
//echo '<br/>'.'insert rec successuflly with : ' . $rawRec['enterprise_name'];
}
$count += 1;
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
echo "已运行 ".$count." 条数据。";
echo "程序结束时间:".date("Y-m-d H:m:s",time())."<br/>";
}
public function parseArgs($argv){
array_shift($argv);
$out = array();
foreach ($argv as $arg){
if (substr($arg,0,2) == '--'){
$eqPos = strpos($arg,'=');
if ($eqPos === false){
$key = substr($arg,2);
$out[$key] = isset($out[$key]) ? $out[$key] : true;
} else {
$key = substr($arg,2,$eqPos-2);
$out[$key] = substr($arg,$eqPos+1);
}
} else if (substr($arg,0,1) == '-'){
if (substr($arg,2,1) == '='){
$key = substr($arg,1,1);
$out[$key] = substr($arg,3);
} else {
$chars = str_split(substr($arg,1));
foreach ($chars as $char){
$key = $char;
$out[$key] = isset($out[$key]) ? $out[$key] : true;
}
}
} else {
$out[] = $arg;
}
}
return $out;
}
.
.
.
.
.
.
(省略的代码)
}
?>
至此Thinkphp代码结束。
然后在命令行输出 php index.php --start=0 --end=9 程序就会自动运行index方法里的相关代码了。
- 用Thinkphp写的简单程序在命令行执行(非 Thinkphp 下的cli模式 而是php下的命令模式)
- 全面总结php在cli命令行模式下的使用
- 命令行模式下使用THINKPHP
- thinkphp 3.2的cli模式(命令行模式)的正确使用方法
- PHP 的命令行模式(CLI)
- 如何让PHP脚本在cli模式下执行时可接受选项和值的输入
- 写个 php 的 hello world 并用 cli 模式执行
- php命令行模式(cli)的使用(总结)
- ThinkPHP的URL模式
- Nginx服务器下使用ThinkPHP的pathinfo模式的问题
- ThinkPHP分组模式下使用RBAC的方法
- php命令行(cli)下的开发
- ThinkPHP + jquery easyui form 提交在IE兼容模式下变文件下载的解决办法
- thinkphp在Nginx下使用PATHINFO模式显示no input file specified的解决方法
- thinkphp找不到common.php下的函数
- THINKPHP rewrite模式在nginx下配置
- linux下PHP的命令行模式介绍
- linux下PHP的命令行模式介绍
- memset的几种用法
- HDU4575/2013年长沙赛区Changsha Marathon
- 桶排序
- 移动互联网应用技术架构简介-Restful服务
- 一年多来的技术历程
- 用Thinkphp写的简单程序在命令行执行(非 Thinkphp 下的cli模式 而是php下的命令模式)
- 多核学习总结
- java基础之内部类
- Ubuntu下mysql中Table is read only的解决
- Windows快捷方式文件格式解析
- 与贺老师的对话
- 冒泡排序、快速排序
- No result defined for action No result defined for action No result defined for action
- HDU4573/2013年长沙赛区Throw the Stones