我在根目录中有一个 PHP 应用程序,其中有 2 个名为“ database.php ”和“ Thread.php ”的文件(已从此处下载 Thread.php )。database.php 在连接到远程 MondoDb 数据库后执行一些琐碎的任务。这是 database.php 的代码:
<?php
include('Thread.php');
$dbUser = 'MyUserName';
$dbPass = 'MyPassword';
$dbHost = 'dsxxxxxx.mongolab.com';
$dbPort = 'xxxxx';
$dbName = 'MyDatabaseName';
$collectionName = 'TestCollection';
function connectToDatabase($dbUser, $dbPass, $dbHost, $dbPort, $dbName) {
try {
reconnect:
echo "connecting to mongodb://{$dbUser}:{$dbPass}@{$dbHost}:{$dbPort}/{$dbName} ...<br/>";
$database = new MongoClient("mongodb://{$dbUser}:{$dbPass}@{$dbHost}:{$dbPort}/{$dbName}");
echo "connected ...<br/>";
}
catch (MongoConnectionException $e) {
goto reconnect;
}
return $database->$dbName;
}
$database = connectToDatabase($dbUser, $dbPass, $dbHost, $dbPort, $dbName);
$collection = $database->$collectionName;
function modifyRecent($id, $error, $time) {
global $database;
echo "In function modifyRecent ...<br/>";
//do something
return;
}
function modifyAll($id, $error, $time) {
global $database;
echo "In function modifyAll ...<br/>";
//do some other things
return;
}
function updateDatabase($id, $error, $time) {
echo "In function updateDatabase ...<br/>";
if( ! Thread::available() ) {
echo "Thread is not supported ...<br/>";
return false;
}
$thread1 = new Thread('modifyAll');
$thread2 = new Thread('modifyRecent');
$thread1->start($id, $error, $time);
$thread2->start($id, $error, $time);
while($thread1->isAlive() || $thread2->isAlive()) {}
return true;
}
updateDatabase((float)"1", (float)"12.6", (float)"23.7");
?>
现在当我从 shell () 运行这个脚本时php database.php
......它显示了一个输出:
user@local_machine:~/AppDir$ php database.php
connecting to mongodb://MyUserName:MyPassword@dsxxxxxx.mongolab.com:xxxxx/MyDatabaseName ...<br/>connected ...<br/>In function updateDatabase ...<br/>In function modifyAll ...<br/>In function modifyRecent ...<br/>
user@local_machine:~/AppDir$
这意味着一切顺利。
但是从浏览器( http://app.localhost/database.php )运行相同的脚本后,输出为:
connecting to mongodb://MyUserName:MyPassword@dsxxxxxx.mongolab.com:xxxxx/MyDatabaseName ...
connecting to mongodb://MyUserName:MyPassword@dsxxxxxx.mongolab.com:xxxxx/MyDatabaseName ...
connecting to mongodb://MyUserName:MyPassword@dsxxxxxx.mongolab.com:xxxxx/MyDatabaseName ...
connecting to mongodb://MyUserName:MyPassword@dsxxxxxx.mongolab.com:xxxxx/MyDatabaseName ...
connected ...
In function updateDatabase ...
Thread is not supported ...
PS:我已经配置了一个虚拟主机(http://app.localhost/),它指向我的应用程序的根目录(~/AppDir),以便在本地对其进行测试。
谁能指出我哪里错了?