How to get query result separated by columns?

Asked

Viewed 194 times

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/>";

  • 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"

  • 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";

2 answers

1

Simply print the column by its respective index.

print ( $row['name']."\n".$row['origin'] );

I don’t know on Linux, but on Windows for \n break the line, it needs the function nl2br.

Staying:

print nl2br( ( $row['name']."\n".$row['origin'] ) );

-1

How about trying with the Freebase.com?

{ "id": null, "name": "Arctic Monkeys", "type": "/music/Artist" }

Freebase also uses Wikipedia data and all requests are in JSON, so it’s easy to ask for any specific information.

  • If the Dbpedia result is an "array"=>"associative" just add foreach($Fields as $key=>$value)

  • sorry Gaucho, but I didn’t understand the solution... could you give this example using the name field that is returned from the query? thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.