php – How to get query result separated by columns?

Question:

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:

<?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";
      }
    }
?>

Answer:

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.

Staying:

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