Domanda

I'm using PHP Mongo with Snow Leopard and I keep getting the following errors:

'undefined variable' and 'trying to get property of non-object'

What I'm trying to do:

Extract variables from a URL (similar to below) to populate a collection within a mongo db. In addition, I'm trying to capture the IP address for the user and date/time the action took place.

Here is an example of a possible URL:

img src="http://localhost:8888/index.php?PIN=123&CID=123&EID=456" width="1" height="1"

PHP/Mongo Driver script:

<?php
  // Connect to MongoDB @ localhost w/ login/pass
  $m = new Mongo("mongodb://login:pass@localhost");

  // Connect to DB
  $db = $m ->admin;

  // Connect to Collection
  $collection = $db ->email_opens;

  // Extract date
  $createdOn = $id->getTimestamp();
  $date_open = date('r', $createdOn);

  // Get variables from URL
  $PIN = $_GET['PIN'];
  $CID = $_GET['CID'];
  $EID = $_GET['EID'];

  // Retrieves user's IP address
  $ip = $_SERVER["REMOTE_ADDR"];

  // Set array
  $obj = array('PIN' => $PIN, 'CID' => $CID, 'EID' => $EID, 'date_open' => $date_open, 'ip' => $ip); 

  // Set index
  $collection -> ensureIndex(array("id" => 1));

  // Insert collection
  $collection -> insert($obj);

  // Probably not necessary
  $connection -> close();
?>
È stato utile?

Soluzione

This is clearly not an MongoDB trouble but only a php one. As Justin Jenkins commented, you are not setting $id before quering on it.

On a more MongoDB part, you are creating a useless (regarding example object you insert) on the id field. You don't need to create an index on generated id, this is automatically done by mongo server. And, the exact name of the field is _id (with an underscore).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top