I just installed MongoDB on my Linux (12.04) with its extension for PHP, and both seems to be well installed (phpinfo() shows infos about MongoDB, enabled etc.).
But when i want to load a page containing the following code, nothing is displayed:
<?php
$m = new MongoClient();
echo "Connection to database sucessfull";
$db=$m->mydb;
echo "Database mydb selected";
?>
Any suggestion ?
Thanks
Actual Syntactic code is this:
$m = new MongoClient('mongodb://localhost', [
'username' => 'abc',
'password' => 'abc#123',
'db' => 'abc'
]);
May be you try once.
Use get_last_error() and dump it on screen and send.
Related
I am trying to insert a document using PHP for my project but the code is not working.
for reference, I have echoed "still here" and "done".
But the code is not able to execute the insert query please help.
I have already tried all the stack overflow and other sites but non of them work properly.
<?php
require 'vendor/autoload.php';
include 'lib/JSON.php';
// connect to mongodb
$client = new MongoDB\Client("mongodb://localhost:27017");
// select a database
$db = $client->hm;
$collection = $db->sc;
$count = $collection->count();
$document = array("_id" => $count,
"device_id" => 100,
"pin" => 17,
"status" => "True");
echo "still here";
$collection->insert($document);
echo "done"
?>
I am only getting "still here" on browser.
Try to use method insertOne. Does that give some additional errors?
https://docs.mongodb.com/php-library/master/reference/method/MongoDBCollection-insertOne/
I am very new to php and mongodb. I have installed php7.0 and mongo 3.4.10 on Ubuntu 16.04.3 LTS.
I can display desired document by entering mongo cli command:
db.testcollection.find({_id:'superid'}).pretty()
It gives me this result:
{ "_id" : "superid", "record" : "whatever" }
But I am tryig to display document from mongodb collection with this php script:
<?php
$mongo = new \MongoDB\Driver\Manager();
$filter = ['_id' => 'superid'];
$options = [];
$query = new \MongoDB\Driver\Query($filter, $options);
$rows = $mongo->executeQuery('db.testcollection', $query);
foreach ($rows as $document) {
print_($document);
var_dump($document);
echo $document;
}
echo "The END"
?>
This only display "The END".
What I am missing in my php script to display mongo query result similarly as cli command?
The problem was:
I have created new databse called test using this commands in mongo shell:
use test;
db.createCollection("testcollection");
db.testcollection.insert({ "_id" : "superid", "record" : "whatever"});
So path to db was test.testcollection not db.testcollection.
When I correct appropriate line like this:
$rows = $mongo->executeQuery('test.testcollection', $query);
It works as expected.
I was confused by mongo shell (cli) that gives me right result without need to specify database name (even after logoff mongo shell and login again).
On the other hand you must specify dbname.collectionname in php (what makes sense) :)
I am trying to use mongodb full text search for showing jobs list. I have done all the necessary steps to create the text indexes and enable the full text search feature on the database and everything is working fine except from the full text search using PHP 5.6.
use the below code in php for full text:
<?php
$username = 'mongodbusername';
$password = 'changeMe';
$m = new MongoClient("mongodb://myadmin1:myadmin123#localhost/dbname");
//$m = new MongoClient("mongodb://localhost", array("username" => $username, "password" => $password,"db" => "jobma_integrations"));
$db = $m->integrations; // this is your dbname
$crawlingCollection = $db->crawled_jobs; // this is your collection name
$c = $crawlingCollection->find(
['$text' => ['$search' => "sales \"ardmore\""]], // this \"ardmore\" is used for exact match and sales will be match with any where
['score'=> ['$meta'=>'textScore']]
)->sort(
['score'=> ['$meta'=>'textScore']]
);
echo "<pre>";
var_dump(iterator_to_array($c));
?>
I am on php 5.6 using WAMP and want to insert a document into MongoDB using PHP. I am doing it in this way:
<?php
require 'vendor/autoload.php';
$con = new MongoDB\Client("mongodb://localhost:27017");
echo "successfully";
$db = $con->selectDatabase('DB');
echo "Selected";
$col = $db->selectCollection('myCol');
$document = array(
"name" => "Deny",
"password" => "1234"
);
$col->insert($document);
echo "successfully";
?>
But it is giving the error
Fatal error: Call to undefined method MongoDB\Collection::insert() in C:\wamp64\www...
I have read http://php.net/manual/en/mongocollection.insert.php and when I use the same insert function, it doesn't work for me.
Instead of MongoDB\Client use MongoClient .
This works for me.
You have to install MongoClient library:
http://php.net/manual/en/mongo.installation.php
Instead of method MongoDB\Collection::insert() using insertOne() or insertMany() would work!
I am trying to get data from MySQL database but in the php file I get this error:
Fatal error: Call to undefined function: json_encode() in /homez.100/pizzapar/www/clic/marwa/test/base.php on line 13
Here is my php file:
<?php
echo"welcom <br>";
$conn = mysql_connect('xxx.xxx.xxx.xxx','xxx','xxx');
if ($conn) {
mysql_select_db('zak', $conn);
$sql=mysql_query("select * from zak_user");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
}
else
{
echo"erreur connexion";
}
Can some one help?
According to the manual, json_encode() is available in PHP >= 5.2.0 only. You are probably running an older version.
See here:
http://www.boutell.com/scripts/jsonwrapper.html
This is a replacement for JSON in earlier PHP versions.
Usage:
require 'jsonwrapper.php';
At the top of your code and you're ready to go. Of course,
jsonwrapper.php must be in the same folder. If not, adjust the require
command.
json_encode example
Just to give you a sense of the possibilities:
$data = array(
array('name' => 'Jane', 'age' => 35),
array('name' => 'Steve', 'age' => 37)
);
<script>
var data = <?php echo json_encode($data) ?>;
</script>
And this is the direct download:
http://www.boutell.com/scripts/jsonwrapper.tar.gz
json_encode used to be not part of PHP, you might have to compile an extension to enable this function.
It's been awhile that json_encode is shipped with PHP(since 5.2), I advise you to upgrade.