/    Sign up×
Community /Pin to ProfileBookmark

Call to a member function createElement() on a non-object

Hello,

I’m getting a error i’ve not seen before, which is: [B]Call to a member function createElement() on a non-object[/B]

This is my php code, It creates a xml document to feed marker info to a google maps api:

[code=php]
<?php

// Start XML file, create parent node

$dom = new DOMDocument(“1.0”);
$node = $dom->createElement(“markers”);
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server

$dbcnx = @mysql_connect(‘xxxxxx’, ‘xxxxxx’, ‘xxxxxx’);
if (!$dbcnx) {
exit(‘<p>Unable to connect to the ‘ .
‘database server at this time.</p>’);
}

if (!@mysql_select_db(‘xxxxxx’)) {
exit(‘<p>Unable to locate the ‘ .
‘database at this time.</p>’);
}

//local search function

if(isset($_GET[‘postcode’]))
{
$postcode_initial = mysql_real_escape_string($_GET[‘postcode’]);
$postcode = trim($postcode_initial);
getDistanceUK_ssc($postcode,100);
}

function getDistanceUK_ssc($postcode,$area)
{
// database query to find postcodes withinh x ($area) distance from submitted postcode
$query = “SELECT postcode1, postcode2, postcode, name, address1, address2, address3, address4, tel, email, website, lat, lng, typ, (SQRT(POW((b.x – a.x), 2) + POW((b.y – a.y), 2))/1000) * 0.621 AS distance
FROM postcodes a, postcodes b, dealers
WHERE typ=’standard’
AND a.outcode LIKE ‘”.$postcode.”‘ AND b.outcode = dealers.postcode1
HAVING (distance < ‘”.$area.”‘)
ORDER BY distance asc LIMIT 10″;

$result = mysql_query($query);

//start the xml
header(“Content-type: text/xml”);

while ($row = @mysql_fetch_assoc($result))
{
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement(“marker”);
$newnode = $parnode->appendChild($node);
$newnode->setAttribute(“name”, $row[‘name’]);
$newnode->setAttribute(“address1”, ucfirst(strtolower($row[‘address1’])));
$newnode->setAttribute(“address2”, ucfirst(strtolower($row[‘address2’])));
$newnode->setAttribute(“address3”, ucfirst(strtolower($row[‘address3’])));
$newnode->setAttribute(“address4”, ucfirst(strtolower($row[‘address4’])));
$newnode->setAttribute(“postcode”, ucfirst(strtolower($row[‘postcode’])));
$newnode->setAttribute(“tel”, ucfirst(strtolower($row[‘tel’])));
$newnode->setAttribute(“fax”, ucfirst(strtolower($row[‘fax’])));
$newnode->setAttribute(“email”, ucfirst(strtolower($row[’email’])));
$newnode->setAttribute(“lat”, $row[‘lat’]);
$newnode->setAttribute(“lng”, $row[‘lng’]);
$newnode->setAttribute(“typ”, $row[‘typ’]);
//add more fields here
}
// if no results are found display message

//$count=mysql_num_rows($result);
//if($count==0)
//{
//no dealers
//}
}

echo $dom->saveXML();

?>
[/code]

(I’ve blanked out the database connections)

This was working fine until i added the local search query, does anyone have any idea what is causing the error?

Thanks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@copesauthorMay 13.2009 — This is the full error:

Fatal error: Call to a member function createElement() on a non-object in markerdata2.php on line 50
Copy linkTweet thisAlerts:
@MindzaiMay 13.2009 — $dom does not exist in the scope of the getDistanceUK_ssc() function. You either need to create an instance of the class inside the function itself, or else pass an object into the function as an argument.
×

Success!

Help @copes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...