Deployed a quick cloud server just to host a MongoDB to tinker with it out of curiosity. It's installed and works. Made a test DB/Table like this:
db.items.insert({ name: 'eggs', quantity: 10, price: 1.50 })
db.items.insert({ name: 'bacon', quantity: 3, price: 3.50 })
db.items.insert({ name: 'tomatoes', quantity: 30, price: 0.50 })
When I run db.items.find({}) all the items appear and all is well.
Now in PHP when I connect to that database from a different server I do this:
// open connection to MongoDB server
$conn = new Mongo('mongodb://theAdmin:Gold1234#165.225.130.252:27017');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// execute query
// retrieve all documents
$cursor = $collection->find();
// iterate through the result set
// print each document
echo $cursor->count() . ' document(s) found. <br/>';
foreach ($cursor as $obj) {
echo 'Name: ' . $obj['name'] . '<br/>';
echo 'Quantity: ' . $obj['quantity'] . '<br/>';
echo 'Price: ' . $obj['price'] . '<br/>';
echo '<br/>';
}
and I get this error:
Fatal error: Uncaught exception 'MongoConnectionException' with
message 'Failed to connect to: 165.225.130.252:27017: Transport
endpoint is not connected' in /home/moosex/public_html/info.php:4
Stack trace: #0 /home/moosex/public_html/info.php(4):
Mongo->__construct('mongodb://[theA...') #1 {main} thrown in
/home/moosex/public_html/info.php on line 4
I've looked up and tried several different ways to connect and still can't get it. How am I supposed to connect remotely?
BTW, that is the actual username password and address to that server(there's nothing on there except for eggs bacon and tomatoes), if you can connect to it, god bless you lol.
To be able to debug "random weirdness" like this, it is very useful to turn on the internal driver logging.
The driver does whole lot of things behind the scenes, and can spit out all sort of important debug information.
Add the following at the top of your script:
<?php
MongoLog::setLevel(MongoLog::ALL);
MongoLog::setModule(MongoLog::ALL);
?>
By default the logger will spew out "php error messages" (E_NOTICE/E_WARNING), if you have error_log enabled, make sure to check that file for the results.
For your (slightly modified) connection string, I get the following results
Notice: PARSE INFO: Parsing mongodb://theAdmin:Gold1234#localhost:27027 in Command line code on line 1
Notice: PARSE INFO: - Found user 'theAdmin' and a password in Command line code on line 1
Notice: PARSE INFO: - Found node: localhost:27027 in Command line code on line 1
Notice: PARSE INFO: - Connection type: STANDALONE in Command line code on line 1
Notice: PARSE INFO: - No database name found for an authenticated connection. Using 'admin' as default database in Command line code on line 1
Notice: CON INFO: mongo_get_read_write_connection: finding a STANDALONE connection in Command line code on line 1
Notice: CON INFO: connection_create: creating new connection for localhost:27027 in Command line code on line 1
Notice: CON WARN: connection_create: error while creating connection for localhost:27027: Invalid argument in Command line code on line 1
Notice: CON WARN: Couldn't connect to 'localhost:27027': Invalid argument in Command line code on line 1
I suspect a firewall issue at either end.. Can you connect to the server using the mongo shell?
Related
I am trying to read visual foxpro .dbf files using php and getting the following error:
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft OLE DB Provider for Visual FoxPro<br/><b>Description:</b> Invalid path or file name.' in C:\xampp\htdocs\phpdbf\index.php:41 Stack trace: #0 C:\xampp\htdocs\phpdbf\index.php(41): com->Open('Provider=VFPOLE...') #1 {main} thrown in C:\xampp\htdocs\phpdbf\index.php on line 2
I have downloaded and run provider from here (note: not sure if I need to do any extra configuration - just simply run it).
Here is my code:
(note: I am not sure about the "ADODB.Connection" and "Provider=VFPOLEDB.1" values in the code. let me know if they don't stand for defaults)
$conn = new COM("ADODB.Connection");
$conn->Open('Provider=VFPOLEDB.1;Data Source="C:\\xampp\\htdocs\\phpdbf;";');
//test.dbf is the file
$rs = $conn->Execute("SELECT * FROM test");
// Display all the values in the records set
while (!$rs->EOF) {
$fv = $rs->Fields("my_datetime");
echo $fv->value."<br/>";
$rs->MoveNext();
}
$rs->Close();
Note: I have tried the answer here , but still getting this
error.
Your data source needs to point at the visual Foxpro .dbc file. Example
Data Source="c:\\vfpdata\\mydatabase.dbc"
I've migrated a site to our hosting, standard ubuntu Plesk 11 server...
The site will not run, it stops at this line, with no error logged, or returned to the screen.
$db=&DB::connect("mysql://$config[db_username]:$config[db_password]#$config[db_host]/$config[db_name]" );
The config array is populated with the correct info for the database. The next few lines of code are
if (PEAR::isError($db)) {
print nl2br(var_export($db));
die("Failed connecting to database");
}
Which the program never gets to? So I'm really stuck. It has the correct info, the database is there, but it does not appear to be able to get any further than the DB::connect line, but isn't showing any error?
Try to add these lines inside your "if block" to find out the error reason:
echo 'Standard Message: ' . $db->getMessage() . "\n";
echo 'Standard Code: ' . $db->getCode() . "\n";
Print additional info:
echo 'DBMS/User Message: ' . $db->getUserInfo() . "\n";
echo 'DBMS/Debug Message: ' . $db->getDebugInfo() . "\n";
Look at this link: http://pear.php.net/manual/en/package.database.db.db-error.php
Well the above didn't return anything either, but I did find that DB has been superceeded by MDB2, so I updated with
require_once('/usr/share/php/MDB2.php');
and
$db =& MDB2::factory("mysql://$config[db_username]:$config[db_password]#$config[db_host]/$config[db_name]" );
Which did get me an error, saying PHP Fatal error: Call to undefined function: MDB2_Driver_mysql::getAll(). in /usr/share/php/MDB2.php on line 1936
So I added this
$db->loadModule('Extended');
From this question Fatal error: Call to undefined function: MDB2_Driver_MYSQL::getAll()
and it seems to have connected. Still not quite there but now I have something on the screen ! Thanks all.
I am conecting to a feed using the STOMP protocol in PHP
Because there are not many messages coming from the feed I believe my script thinks I have lost connection and the below error pops out:-
Uncaught exception 'StompException' with message 'Unexpected EOF while reading from socket'
If I can send a heart beat I believe the script will keep running - ie know I'm still there
The code is below. My problem is that I am getting errors. See NEW EDIT below
$channel="VSTP_ALL";
$con=new Stomp($server,$user,$password,array('heart-beat'=>'0,20000'));
if (!$con) {
die('connection failed: ' .stomp_connect_error());
}
$con->subscribe("/topic/".$channel);
if ($con->hasFrame()){
$msg=$con->readFrame();
foreach (json_decode($msg->body) as $event) {
var_dump($event);
}
$con->ack($msg);
}
die ('connection lost:'.time());
?>
EDIT
Have edited above to move/change syntax of array('heart-beat','0,20000') to array('heart-beat'=>'0,20000')
However I now get errors as follows in the foreach line:-
Notice: Trying to get property of non-object in
Warning: Invalid argument supplied for foreach()
and then in the ack($msg) line
Warning: Stomp::ack(): Expects parameter 2 to be a string or a StompFrame object. in
i keep getting the error for this code even though it works as it should when i run it in browser, but when called by include_once it doesnt work due to the error
foreach(($hostlist->uploaded) as $uploaded) {
if (strcmp($uploaded->url,"http://someurl.com/")==0) {
$host = simplexml_load_file($config['hostlist']);
unset($host->uploaded->url);
unset($host->uploaded->pass);
$host->uploaded->addChild('url',"http://anotherurl.com/");
$host->uploaded->addChild('pass',"anotherpass");
$host->uploaded->asXML($config['hostlist']);
$host->asXML($config['hostlist']);`
echo "URL Changed to http://anotherurl.com/";
}
}
knowing that the variables are as follows:
$config['hostlist'] = 'xml/host.xml';
$hostlist = simplexml_load_file($config['hostlist']);
and this is a sample of the xml file:
<host>
<uploaded>
<work>yes</work>
<url>http://someurl.com/</url>
<pass>pass</pass>
</uploaded>
</host>
As I had early mentioned via comments, your issue maybe that the file is using a relative path and being accessed via include_once by a script on a different folder, which causes the file path of your XML to be invalid.
Here is a simple example of what may be happening to you.
I have the following folder structure:
root
- include_folder/
- include_folder/read_host.php
- include_folder/xml
- include_folder/xml/host.xml
- test_xml.php
When accessing include_folder/read_host.php it reads the file just fine.
This is my read_host.php:
<?php
$config['hostlist'] = 'xml/host.xml';
$hostlist = simplexml_load_file($config['hostlist']);
foreach($hostlist->uploaded as $uploaded)
{
echo $uploaded->work, "\n";
echo $uploaded->url, "\n";
echo $uploaded->pass, "\n";
}
The output:
yes
http://someurl.com/
pass
However if I access from test_xml.php which have the following content:
<?php
include_once('include_folder/read_host.php');
echo "what happens";
It fails with error:
PHP Warning: simplexml_load_file(): I/O warning : failed to load external entity "xml/host.xml" in /home/admin/include_folder/read_host.php on line 4
PHP Notice: Trying to get property of non-object in /home/admin/include_folder/read_host.php on line 5
PHP Warning: Invalid argument supplied for foreach() in /home/admin/include_folder/read_host.php on line 5
what happens
However if I change $config['hostlist'] = 'xml/host.xml'; to the absolute path to the XML file it works just fine and output:
yes
http://someurl.com/
pass
what happens
So in my case the absolute folder was:
$config['hostlist'] = '/home/admin/include_folder/xml/host.xml';
I get a notice from my php-script:
Undefined offset: 32 in C:\xampp\htdocs\WWW\myfilexyz.php on line 74
I want to detect where the error occurs as the procedure which produces the error is called many times.
I added the following line of code:
error_log("you made a mistake", 3, "errorfile.log");
Looking into errorfile.log, the message appears 3 times, but the procedure is called more than 100 times. How can I find the 3 lines where the error is produced?
I would like to see sometime like:
"you made a mistake, called from line 234 from start.php"
The easiest is to use a proper error handler which provides a stacktrace for each error. You can install xdebug, which outputs a lot more details about an error when activated. While you're at it, look into using xdebug to hook up a debugger (read xdebug's documentation).
Alternatively write your own error handler which uses debug_backtrace to give you a decent stacktrace.
$err = error_get_last();
error_log($err['message'] . ' in ' . $err['file'] . ' on line' . $err['line'], 3, "errorfile.log");