function query_bm($ref)
{
    $textRefs = array();
    $db = sparql_connect("http://collection.britishmuseum.org/sparql");
    if (!$db) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    sparql_ns("bmo", "http://collection.britishmuseum.org/id/ontology/");
    sparql_ns("ecrm", "http://erlangen-crm.org/current/");
    sparql_ns("object", "http://collection.britishmuseum.org/id/object/");
    $sparql = 'SELECT DISTINCT ?coin ?text WHERE {
  ?coin ecrm:P70i_is_documented_in <REF> . FILTER regex(str(?coin), "C[A-Z]{2}[0-9]+") .
  ?coin a ecrm:E22_Man-Made_Object ;
        ecrm:P50_has_current_keeper <http://collection.britishmuseum.org/id/thesauri/department/C> ;
        bmo:PX_display_wrap ?text . FILTER regex(?text, "Bibliograpic\\\\sreference")
  } LIMIT 10';
    $result = sparql_query(str_replace('REF', $ref, $sparql));
    if (!$result) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    $fields = sparql_field_array($result);
    while ($row = sparql_fetch_array($result)) {
        $textRefs[$row['coin']][] = $row['text'];
        /*foreach( $fields as $field )
        		{
        			$textRefs
        		}*/
    }
    return $textRefs;
}
function query_bm($writer, $uri)
{
    $db = sparql_connect("http://collection.britishmuseum.org/sparql");
    if (!$db) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    sparql_ns("thesDimension", "http://collection.britishmuseum.org/id/thesauri/dimension/");
    sparql_ns("bmo", "http://collection.britishmuseum.org/id/ontology/");
    sparql_ns("ecrm", "http://erlangen-crm.org/current/");
    sparql_ns("object", "http://collection.britishmuseum.org/id/object/");
    $sparql = "SELECT ?image ?weight ?axis ?diameter ?objectId ?hoard WHERE {\n  OPTIONAL {<OBJECT> bmo:PX_has_main_representation ?image }\n  OPTIONAL { <OBJECT> ecrm:P43_has_dimension ?wDim .\n           ?wDim ecrm:P2_has_type thesDimension:weight .\n           ?wDim ecrm:P90_has_value ?weight}\n  OPTIONAL {\n     <OBJECT> ecrm:P43_has_dimension ?wAxis .\n           ?wAxis ecrm:P2_has_type thesDimension:die-axis .\n           ?wAxis ecrm:P90_has_value ?axis\n    }\n  OPTIONAL {\n     <OBJECT> ecrm:P43_has_dimension ?wDiameter .\n           ?wDiameter ecrm:P2_has_type thesDimension:diameter .\n           ?wDiameter ecrm:P90_has_value ?diameter\n    }\n  OPTIONAL {\n     <OBJECT> ecrm:P1_is_identified_by ?identifier.\n     ?identifier ecrm:P2_has_type <http://collection.britishmuseum.org/id/thesauri/identifier/codexid> ;\n        rdfs:label ?objectId\n    }\n  OPTIONAL {\n     <OBJECT> bmo:PX_display_wrap ?hoard . FILTER regex(?hoard, 'IGCH')\n    }\n  }";
    $result = sparql_query(str_replace('OBJECT', $uri, $sparql));
    if (!$result) {
        print sparql_errno() . ": " . sparql_error() . "\n";
        exit;
    }
    $fields = sparql_field_array($result);
    while ($row = sparql_fetch_array($result)) {
        foreach ($fields as $field) {
            if (strlen($row[$field]) > 0) {
                if ($field == 'hoard') {
                    preg_match('/IGCH\\s([0-9]+)/', $row[$field], $matches);
                    if (isset($matches[1])) {
                        $num = str_pad($matches[1], 4, "0", STR_PAD_LEFT);
                        $hoardURI = 'http://coinhoards.org/id/igch' . $num;
                        echo "Found hoard {$hoardURI}\n";
                        $writer->startElement('dcterms:isPartOf');
                        $writer->writeAttribute('rdf:resource', $hoardURI);
                        $writer->endElement();
                    }
                } else {
                    switch ($field) {
                        case 'image':
                            $writer->startElement('foaf:depiction');
                            $writer->writeAttribute('rdf:resource', $row[$field]);
                            $writer->endElement();
                            break;
                        case 'objectId':
                            $writer->startElement('foaf:homepage');
                            $writer->writeAttribute('rdf:resource', "http://www.britishmuseum.org/research/collection_online/collection_object_details.aspx?objectId={$row[$field]}&partId=1");
                            $writer->endElement();
                            break;
                        case 'axis':
                            $writer->startElement('nmo:hasAxis');
                            $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#integer');
                            $writer->text($row[$field]);
                            $writer->endElement();
                            break;
                        case 'weight':
                            $writer->startElement('nmo:hasWeight');
                            $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#decimal');
                            $writer->text($row[$field]);
                            $writer->endElement();
                            break;
                        case 'diameter':
                            $writer->startElement('nmo:hasDiameter');
                            $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#decimal');
                            $writer->text($row[$field]);
                            $writer->endElement();
                            break;
                    }
                }
            }
        }
    }
}
 public function getEnglishResourceFromEnglishWikipediaLink($englishWikipediaLink)
 {
     $result = null;
     $db = sparql_connect('http://dbpedia.org/sparql');
     $query = "select * where { ?resource foaf:isPrimaryTopicOf <{$englishWikipediaLink}> .}";
     //	print_r($query);
     $resultQuery = sparql_query($query);
     if ($resultQuery) {
         $fields = sparql_field_array($resultQuery);
         $row = sparql_fetch_array($resultQuery);
         if ($row != null) {
             $result = $row["resource"];
         }
     }
     return $result;
 }
        <h3>Dados trazidos de <a href="http://dbpedia.org/">dbpedia.org</a></h3>
		<?php 
require_once "sparqllib.php";
$db = sparql_connect("http://dbpedia.org/sparql/");
if (!$db) {
    print "<p class=\"error\">" . sparql_errno() . ": " . sparql_error() . "</p>";
    exit;
}
sparql_ns("foaf", "http://xmlns.com/foaf/0.1/");
$sparql = "select distinct ?Estado ?Populacao where {\n                ?estados <http://dbpedia.org/ontology/type> <http://dbpedia.org/resource/States_of_Brazil>. \n                ?estados <http://dbpedia.org/property/name> ?Estado. \n                ?estados <http://dbpedia.org/property/populationTotal> ?Populacao.\n                } ORDER BY DESC(?Populacao)";
$result = sparql_query($sparql);
if (!$result) {
    print "<p class=\"error\">" . sparql_errno() . ": " . sparql_error() . "</p>";
    exit;
}
$fields = sparql_field_array($result);
print "<table>";
print "<caption>População dos Estados brasileiros</caption>";
print "<tr>";
foreach ($fields as $field) {
    print "<th scope=\"col\">{$field}</th>";
}
while ($row = sparql_fetch_array($result)) {
    print "<tr>";
    foreach ($fields as $field) {
        $uf = str_ireplace("State", "", "{$row[$field]}");
        $uf = str_ireplace(" of", "", "{$uf}");
        $uf = str_ireplace("Brazilian Federal District", "Distrito Federal", "{$uf}");
        print "<td>{$uf}</td>";
    }
    print "</tr>";