我正在嘗試將一些管理腳本添加到Zend Framework項目中,這將最終通過cron每晚運行。使用Zend_Db運行php作爲cli腳本時出現的問題
但是我試圖使用Zend_Db時遇到了我的第一個問題。我目前在做一個非常簡單的SQL調用來獲取一定的成效,只是使用的var_dump()顯示它們但是我收到以下錯誤信息:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
腳本到目前爲止是這樣的:
<?php
chdir(dirname(__FILE__));
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
date_default_timezone_set('Europe/London');
//directory setup and class loading
set_include_path('.' . PATH_SEPARATOR . '../library/' . PATH_SEPARATOR . '../application/models/'
. PATH_SEPARATOR . '../library/MyApp/'
. PATH_SEPARATOR . get_include_path());
include "Zend/Loader/Autoloader.php";
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
//start sessions
Zend_Session::start();
//read configuration data
$config = new Zend_Config_Xml('../application/config/config.xml', 'app');
//we create an ability to capture errors and write them to an error log if there are problems.
$log = new Zend_Log();
$writer = new Zend_Log_Writer_Stream($config->log->logfile);
$log->addWriter($writer);
$filter = new Zend_Log_Filter_Priority((int)$config->log->level);
$log->addFilter($filter);
//we now need to get the list of graduates that need to have their CV's removed from the search engine
$date = new Zend_Date();
$date->sub(3, Zend_Date::MONTH);
echo $date->get(Zend_Date::ISO_8601);
$sql = "SELECT userid, guid FROM users WHERE lastlogin = ? AND active = 1";
$db = Zend_Db::factory($config->database);
try{
$results = $db->fetchAll($sql, $date->get(Zend_Date::ISO_8601));
var_dump($results);
}catch(Zend_Db_Exception $dbe){
$log->debug($dbe->getMessage()."\n".$dbe->getTraceAsString());
}
//close database.
if($db->isConnected()){
$db->closeConnection();
}
我錯過了什麼?我知道Zend_Config對象中的數據庫連接設置正常工作,並且Web應用程序運行正常,沒有問題。
我現在正在使用Zend Framework v1.7.6來運行它。
也有任何在cli腳本中使用Zend Framework的一般技巧?
非常感謝。
pH值謝謝我知道這將是簡單的事情... – 2010-09-08 13:35:03