dongya6381 2010-09-08 13:06
浏览 69
已采纳

在运行php作为cli脚本时使用Zend_Db的问题

I am trying to add some administration scripts to a Zend Framework project, that will end up being run nightly via cron.

However I've hit my first problem with the script when trying to use Zend_Db. I am currently doing a very simple SQL call to get some results and just display them using var_dump() however I get the following error message:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

The script so far looks like this:

<?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()."
".$dbe->getTraceAsString());

}


//close database.
if($db->isConnected()){
    $db->closeConnection();
}

What am I missing? I know that the database connection settings in the Zend_Config object works and the web application is running fine with no issues.

I am running this using Zend Framework v1.7.6 at present.

Also are there any generic tips for using the Zend Framework in a cli script?

Many thanks.

  • 写回答

2条回答 默认 最新

  • duanshan2988 2010-09-08 13:15
    关注

    PHP will try to connect to mysql via tcp/ip, unless the mysql server is on the same machine as the script and "localhost" is used as the db host. Then it will use a filesystem socket.

    It could be that mysql cannot create /tmp/mysql.sock due to permissions, or that it's not configured to do so. The Mysql manual should have more on that.

    A quick way to get around this for now should be to change the db host in your Zend_Db settings to an actual ip mysql listens on, not "localhost" or "127.0.0.1".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么