How to export data from mongodb without displaying headers? - php

I'm trying to display stored data form the mongodb using the php? But it display's all the header file including data. How to ignore the header information?
It shows like
{ "_id" : ObjectId("550ee694c5c9f2729b066c23"),
I want result as
550ee694c5c9f2729b066c23
my php code:
$db = new Mongo();
$query = $db->selectDB('test');
$collections = new MongoCollection($query,'demo');
$coursor = $collections->find();
foreach ($coursor as $doc)
{print_r($doc);}

You can find the value of the ObjectId() object as a lowercase hexadecimal string using valueOf() method.
ObjectId("550ee694c5c9f2729b066c23").valueOf() = 550ee694c5c9f2729b066c23
For more details you can check documentation :
http://docs.mongodb.org/manual/reference/method/ObjectId.valueOf/

Actually { print_r($doc); } shows header too. There should be some kind of a Key to know which data we need. So we cannot remove them.
$db = new Mongo();
$query = $db->selectDB('test');
$collections = new MongoCollection($query,'demo');
$coursor = $collections->find();
foreach ($coursor as $doc)
{
echo $doc['id'];
}
Will give you only 550ee694c5c9f2729b066c23

Related

How to retrieve MongoBinData to a string in PHP?

I have successfully stored an image into a MongoDB database, since it is stored as a Base64 type information; I want to retrieve that BinData to a string. How do I do that? Here I am finding my document using input 'Email'.
<?php
$m = new MongoClient();
$db = $m->mydb2->mycol2;
$result = $db->find(array('Email'=>$em));
foreach( $result as $key){
$susername = $key['Email'];
$imagebody = $key['pic'];
}
echo $imagebody;
?>
EDIT:
As Haresh has said
$imagebody = $key['pic']->bin
works perfectly. But it returns me something like Raw data but if I write this
$imagebody = base64_encode($key['pic']->bin);
then it returns me exact Base64 format.
According to Documentation
To access the contents of a MongoBinData, use the bin field which return the string Mongo Binary Data
So Try this:
$imagebody = $key['pic']->bin
Hope this works for you.

How to retrieve data from mongodb table in php?

This is my code in php
public function showData(){
$m = new MongoClient();
$db = $m->newdb;
$collection = $db->createCollection("signup");
$data = $collection->find();
echo "<pre>";print_r($data);exit;
}
but it is returning an empty array while i can see the data in the shell. As we can get the data from mongo shell by writing the command db.signup.find().forEach(printjson);
Every time the function runs, it's trying to create a collection that already exists (unless you delete it before and this part of your code is a re-creating logic - but then it would really be empty at this point).
You can access it with selectCollection():
$collection = $db->selectCollection("signup");

Json response with php

I am trying to get JSON response using PHP. I want to have Json array not the HTML tags. But the output shows HTML tags as well.I want to remove this HTML output! PHP code is as follows: I don't know how to do this ? Please help.
Thanks in advance :)
<?php
function getFixture(){
$db = new DbConnect();
// array for json response of full fixture
$response = array();
$response["fixture"] = array();
$result = mysql_query("SELECT * FROM fixture"); // Select all rows from fixture table
while($row = mysql_fetch_array($result)){
$tmp = array(); // temporary array to create single match information
$tmp["matchId"] = $row["matchId"];
$tmp["teamA"] = $row["teamA"];
$tmp["teamB"] = $row["teamB"];
array_push($response["fixture"], $tmp);
}
header('Content-Type: application/json');
echo json_encode($response);
}
getFixture();
?>
It's difficult to tell without seeing what your output is, but there is nothing in your code which would add HTML to your response.
It sounds like the HTML is in the database, so you're getting the data as expected, and your browser is the displaying whatever html elements might be there.
You could ensure none of the rows from the database have HTML in them by using strip_tags as follows:
$tmp["teamA"] = strip_tags($row["teamA"]);
Do this for all rows which may contain html.
Sorry if this is not formatted right, I'm new to StackOverflow!
http://php.net/strip-tags

Session issue in PHP

I am trying to build a simple shopping cart using PHP and no Database.
Based on the user input, I search for an Item using ebay API(returns XML Data).
I am able to get its price,id,and other details.
I am then creating an array called ITEMS which contains all the data returned by ebay.
(I know I have to create a cart only for items that are selected).
The Issue is I am able to access the cart contents from the session within the search function but not from other functions.I am new to PHP could someone help me fix this.
As of now I am only trying to access the cart contents from buy.
function search(){
$xml = new SimpleXMLElement($xmlstr);
print "<table border=1>";
$loop = $xml->categories[0]->category->items->product;
foreach ( $loop as $dummy) {
$id = $dummy->attributes();
$link = $dummy->productOffersURL;
$name = $dummy->name;
$price = $dummy->minPrice;
$image = $dummy->images->image->sourceURL;
}
array_push($ITEMS, $item);
}
}
I changed the for eeach loop and it worked:
The code is :
$id = (String) $dummy->attributes();
$link = (String)$dummy->productOffersURL;
$name = (String)$dummy->name;
$price = (String)$dummy->minPrice;
$id = (String) $dummy->attributes();
$link = (String)$dummy->productOffersURL;
$name = (String)$dummy->name;
$price = (String)$dummy->minPrice;
You shouldn't call session start with the # (error supression) modifier. It could be triggering and error and you wouldn't know.
Check if your php.ini configuration is set to autostart the session.
session.auto_start = 0
If it's set to 1, you could be overwriting its contents by manually calling session start every time.

How Do I Create a JSON Feed from a MongoDB Collection

I'm creating a CMS for my client to work with his photographs and to sell them on his site. For the CMS end as well as the front end, which both will be all AJAX, it'd be nice to get a JSON feed setup so that I can just use the same feed to generate new "pages" and "views" with JS.
So this example feed would have like {[name:'A Photo',description:'lorem ipsum...'],[...]} and then with jQuery or JS i can create a table of all his photographs, pages, etc. How can I set this up for myself?
Should I just create a PHP file that gets all the data from the MongoDB put's it in an array than convert the array to JSON?
$cursor = $this->collection->find($params);
$return = array();
$i=0;
while( $cursor->hasNext() )
{
$return[$i] = $cursor->getNext();
// key() function returns the records '_id'
$return[$i++]['_id'] = $cursor->key();
}
return json_encode($return);
That is how I return JSON frrom Mongo.
I did it this way:
$cursor = $collection->find($params);
if($cursor->hasNext())
{
return json_encode(iterator_to_array($cursor));
}
If you need the _ids, check this answer to see why you're not getting them:
MongoDB PHP: How do I get ObjectId with a JSON feed? (it's blank)
This is what I do :
$data = json_decode(json_encode(iterator_to_array($cursor, true)));
this way I make sure I have an array of objects that you can properly call :
foreach($data as $doc){
echo 'title:'.$doc->title.'<br>';
}

Categories