0

I am getting the error: Message: layerName parameter is not passed in GetPOI request. Fatal error: Call to a member function prepare() on a non-object

here's where i create the PDO $db

function connectDb() {
  try {
    $dbconn = 'mysql:host=' . DBHOST . ';dbname=' . DBDATA ; 
    $db = new PDO($dbconn , DBUSER , DBPASS );
    // set the error mode to exceptions
    $db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);
     return $db;

      }//try
       catch(PDOException $e) {
    error_log('message:' . $e->getMessage());
  }// catch

}// connectDb

and here is where the error occurs:

// $sql is returned as a PDO statement object. 
  $sql = $db->prepare( 'SELECT id,
               imageURL,
               title,
               description,
               footnote,
               lat,
               lon,
               (((acos(sin((:lat1 * pi() / 180)) * sin((lat * pi() / 180)) +
                      cos((:lat2 * pi() / 180)) * cos((lat * pi() / 180)) * 
                      cos((:long  - lon) * pi() / 180))
                      ) * 180 / pi()
               )* 60 * 1.1515 * 1.609344 * 1000
               ) as distance,
               iconID,
               objectID,
               transformID
              FROM POI
         WHERE POI.poiType = "geo"   
        HAVING distance < :radius
      ORDER BY distance ASC
         LIMIT 0, 50 ' );

have I not set up the $db correctly? I can also post the whole script if it helps. Thanks.

1
  • You don't need to return $db. Commented Jun 11, 2014 at 18:27

1 Answer 1

1

The problem is that $db is not defined by the time you call $db->prepare() due to its scope.

It's most likely that $db>prepare() is being called inside a function; if that's the case you fix it by passing $db as an argument to that function, i.e.

function doSomething(\PDO $db)
{
    $sql = $db->prepare('...');
}

$db = connectDb();
doSomething($db);

Another potential issue is that an error occurred in connectDb(); you only log the error, but the function still returns normally and thus $db would still be undefined in the global scope.

Sign up to request clarification or add additional context in comments.

3 Comments

I was just going to make a comment about it most likely being a scope issue, since OP's constants don't seem to be defined anywhere.
Constants have no scope, so that shouldn't be an issue :)
@Jack, except class constants. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.