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!
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'm having a problem with an array in php, which I use to store configuration data for different environments. The idea is that I only have to switch out the file with the config data when going from local testing to using it on the server. The file looks like this:
<?php
$_ENV = array(
'MySQL' => array(
'database'=>'<DATABASENAME>',
'server'=>'<SERVERNAME>',
'username'=>'<USERNAME>',
'password'=>'<PASSWORD>'
)
);
?>
And I include it this way:
include('../env.php');
For the connection to the database I use it like this:
$conn = mysql_connect($_ENV['MySQL']['server'], $_ENV['MySQL']['username'],$_ENV['MySQL']['password']) or die("No connection possible: " . mysql_error());
Ignoring the fact that I'm still using mysql instead of PDO or mysqli, which is a thing I'm going to change soon, this should work. But it does not:
Notice: Undefined index: MySQL in D:\data\dev\applications\wtc-feedback\dashboard\include\SQLrequest.php on line 36
The strange thing is that when I dump the environment variable onto the screen with var_dump() the indexes and the contents are all just fine.
Does anyone know how to fix this?
If You want to work with environmental variables use getenv, putenv
or use .env files (package here, docs here) and add .env to .gitignore
in fact Your code must be like this:
env.php:
<?php
putenv('DB_NAME=DATABASENAME');
putenv('DB_HOST=SERVERNAME');
putenv('DB_USER=USERNAME');
putenv('DB_PASS=PASSWORD');
connection.php:
<?php
include('../env.php');
$conn = mysql_connect(getenv('DB_HOST'), getenv('DB_USER'), getenv('DB_PASS'))
OR die("No connection possible: " . mysql_error());
".env" way:
.env file:
DB_NAME=DATABASENAME
DB_HOST=SERVERNAME
DB_USER=USERNAME
DB_PASS=PASSWORD
connection.php:
<?php
require_once('../vendor/autoload.php');
use Symfony\Component\Dotenv\Dotenv;
(new Dotenv())->load(__DIR__.'/../.env');
$conn = mysql_connect(getenv('DB_HOST'), getenv('DB_USER'), getenv('DB_PASS'))
OR die("No connection possible: " . mysql_error());
Simple solution wouuld be, return the simple array from the file. as
<?php
return array(
'MySQL' => array(
'database'=>'<DATABASENAME>',
'server'=>'<SERVERNAME>',
'username'=>'<USERNAME>',
'password'=>'<PASSWORD>'
)
);
?>
In other file
$credentials = require_once "../env.php";
Use the $credentials array to supply parameters to mysql connection function
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.
I'm trying to query a mongodb database with the _id stored in a session variable...
getting the _id and store as session variable:
$_SESSION['user']['userid']=$user['_id']->{'$id'};
querying db - error on this line:
$user=collection->findOne(array('_id' => new MongoId($_SESSION['user']['userid'])));
can't figure it out.
Should be $user=$collection->findOne(array('_id' => new MongoId($_SESSION['user']['userid']))); You missed a $ infront of collection
<?php
$users= $mongo->my_db->users;
$user = $users->findOne(array('_id' => new MongoId($_SESSION['user']['userid']));
print_r($user);
?>
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.