3
I need to make a query in dbpedia and from this query return name, gender and other information of a particular singer. I am able to do this, but my code returns the whole tuple, I wish I could separate the information by column. Can someone help me? Follows the code:
<?php
    require_once('sparqllib.php');
    $db = sparql_connect('http://dbpedia.org/sparql');
    $query = "  PREFIX owl: <http://www.w3.org/2002/07/owl#>
                PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
                PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX foaf: <http://xmlns.com/foaf/0.1/>
                PREFIX dc: <http://purl.org/dc/elements/1.1/>
                PREFIX : <http://dbpedia.org/resource/>
                PREFIX dbpedia2: <http://dbpedia.org/property/>
                PREFIX dbpedia: <http://dbpedia.org/>
                PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
                PREFIX owl: <http://dbpedia.org/ontology/>
                PREFIX rsc: <http://dbpedia.org/resource/>
                    SELECT ?name ?hometown ?origin ?genre ?bandMember ?currentMembers ?associatedMusicalArtist
                    WHERE { 
                      rsc:Arctic_Monkeys dbpedia2:name ?name.
                      rsc:Arctic_Monkeys owl:hometown ?hometown.
                      rsc:Arctic_Monkeys dbpedia2:origin ?origin .
                      rsc:Arctic_Monkeys dbpedia2:currentMembers ?currentMembers .
                      rsc:Arctic_Monkeys owl:genre ?genre .
                      rsc:Arctic_Monkeys owl:bandMember ?bandMember .
                      rsc:Arctic_Monkeys owl:associatedMusicalArtist ?associatedMusicalArtist .
                    FILTER ((LANG(?name) = 'en') AND (LANG(?origin) = 'en')).
                    }";
    $result = sparql_query($query);
    $fields = sparql_field_array($result);
    while($row = sparql_fetch_array($result))
    {
      foreach($fields as $field)
      {
        print"$row[$field] \n";
      }
    }
?>
You mean this? echo $Row['name']." ( ".$Row['origin']." ) <br/>";
– Dalton Menezes
That’s right, Dalton, that’s right! But I did the test by swapping "print"$Row[$field] n";" for "print"$Row['name'] n";" and it didn’t work... =/ gave the error "Parse error: syntax error, Unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING"
– Gustavo Nunes
try it like this:
print $row[$field]."<br>\n";(with or without the<br>, depending on the desired result). Alternatively, so:print $field.': '.$row[$field]."<br>\n";– Bacco