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

发布于 2024-09-17 20:28:17 字数 2145 浏览 6 评论 0原文

我正在尝试向 Zend Framework 项目添加一些管理脚本,这些脚本最终将通过 cron 每晚运行。

然而,当我尝试使用 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 的通用技巧吗?

非常感谢。

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()."\n".$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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦浮鱼 2024-09-24 20:28:17

PHP 将尝试通过 tcp/ip 连接到 mysql,除非 mysql 服务器与脚本位于同一台机器上并且使用“localhost”作为数据库主机。然后它将使用文件系统套接字。

可能是 mysql 由于权限原因而无法创建 /tmp/mysql.sock,或者未配置为这样做。 Mysql手册应该有更多相关内容。

目前解决这个问题的一个快速方法应该是将 Zend_Db 设置中的数据库主机更改为 mysql 侦听的实际 ip,而不是“localhost”或“127.0.0.1”。

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".

¢蛋碎的人ぎ生 2024-09-24 20:28:17

还要检查 php.ini 并查找 mysql_sock,

这对于安装了 Mamp 的 Mac 用户来说通常是一个陷阱:Snow Leopard 附带了一个 php 配置,该配置与 Mamp 包含的 ph 是分开的

also check your php.ini and look for the mysql_sock

this is often a trap for Mac users with Mamp installed: Snow Leopard comes with a php configured which is separate to the ph included with Mamp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文