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/
Related
I'm trying to update a note(s) that are returned from an Evernote search using PHP. The search part works just fine, but once I find the list of notes I can't seem to add a tag to the note. I can confirm that tag GUIDs are correct in both cases.
use EDAM\NoteStore\NoteFilter;
$client = new Client(array(
'token' => $authToken,
'sandbox' => true
));
$filter = new NoteFilter();
$filter->words = "HIGH";
$filter->tagGuids = array("ababe33d-75e6-4a50-b2ba-61889bb2b8a6");
$notes_result = $client->getNoteStore()->findNotes($filter, 0, 10);
$notes = $notes_result->notes;
foreach ($notes as $note) {
echo $note->title . "\n";
// **** ERROR HERE ****
$note->updateTag($authToken, "b3b290fa-4ca0-493e-8dd1-5cf1bff28b92");
}
The error you get is because updateTag must be called on NoteStore, not the note object. But: NoteStore.updateTag updates the tag object itself. To add tag to a note one needs NoteStore.updateNote instead. Your API token must also support write permission on notes.
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 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 use Codebird to show my latest tweet on a simple website. Unfortunately, I can't manage to make it work.
Here's what I did for now.
I created my App on Twitter Developer page. I obtained my Key/Secret and then my Token/Secret. Then I wrote my small PHP script and tried to show the timeline just to see if everything works. Here I encountered the problems. The code goes like this:
<?php
require_once ('codebird.php');
\Codebird\Codebird::setConsumerKey(MY_KEY, MY_SECRET);
$cb = \Codebird\Codebird::getInstance();
$cb->setToken(MY_TOKEN, MY_TOKEN_SECRET);
$reply = (array) $cb->statuses_homeTimeline();
print_r($reply);
?>
(and obviously I put the various key strings in the correct arguments).
This code gives my an Array ( [httpstatus] => 0 ). So I tried
print_r($reply[0]);
But then nothing is printed out in the page.
Where am I wrong? How should I modify this code to get my last tweet? I'm a bit new with the new Twitter API, and a lot of stuff confuses me.
Thank you for your help!
I copied and pasted your code and it's not working.
I get error regarding the CodeBird class
Try:
require_once ('codebird.php');
Codebird::setConsumerKey('key', 'secret key');
$cb = Codebird::getInstance();
$cb->setToken('token', 'secret token');
$reply = (array) $cb->statuses_homeTimeline();
this should work just fine.
<?php
use Codebird\Codebird;
require 'vendor/autoload.php';
$cb = new Codebird;
$cb->setConsumerKey(
'Consumer_Key',
'Consumer_Secret'
);
$cb->setToken(
'Access_Token',
'Access_Token_Secret'
);
$reply = (array)$cb->statuses_homeTimeline();
echo '<pre>';
print_r($reply);
So, I've been working on this code for some time, but I'm giving up, and reaching out for help from you guys. I've been looking at documentation for MongoDB and PHP, but I can't find anything. What I want to do is take the sample code for inserting a record:
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
And re-purpose is for my project (as shown):
$obj = array( $startCol => $startRow );
The thing is that $startCol and $startRow are arrays, and it gives me a problem every time I want to run the document. Is there something ridiculously simple I'm missing here? Thanks in advance.
Chunk of code that's giving me problems:
$maxRows= count($currentarray); //Outputs 45
$maxCols= count($currentarray[0]); //Outputs 9
$currentRow=1;
$currentCol=1;
$testing = 1;
do {
while ($currentCol<$maxCols){
$startCol[] = $currentarray[0][$currentCol];
$startRow[] = $currentarray[$currentRow][$currentCol];
$currentCol++;
}
$obj = array( $startCol => $startRow );
$collection->insert($obj);
print_r ($collection);
if ($currentCol==$maxCols)
$currentCol=1;
$currentRow++;
$testing++;
//echo "<br />";
} while ($currentRow<$maxRows);
The problem I was getting was with my output statement. So my answer was fairly simple. After, print_r() all of my variables I found that I was in fact storing them, but I was calling them incorrectly at the end of the program.