php – How to get query result separated by columns?


I need to make a query on dbpedia and from this query return name, gender and other information for a particular singer. I'm managing to do this, but my code returns the whole tuple, I wish I could separate the information by column. Can someone help me? Here's the code:

    $db = sparql_connect('');
    $query = "  PREFIX owl: <>
                PREFIX xsd: <>
                PREFIX rdfs: <>
                PREFIX rdf: <>
                PREFIX foaf: <>
                PREFIX dc: <>
                PREFIX : <>
                PREFIX dbpedia2: <>
                PREFIX dbpedia: <>
                PREFIX skos: <>
                PREFIX owl: <>
                PREFIX rsc: <>
                    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";


Just print the column by its respective index.

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

I don't know on Linux, but on Windows to \n break the line, you need the nl2br function.


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