View database data

0

I have a bunch of data with information from various bands, however I would like to pull them to my front page.

Bank name: dados

Table called: Artists

where I have the information in it:

  • imagem_small (in this case I use url stored from the images of the bands)
  • Name - band name
  • spotify_popularity - band popularity

The idea would be to show the above image and artist name below in a column with 7 artists and that only bands with popularity under 20 would appear in the random column.

would be like this site link

This code is what I'm using to pull the content. localhost I'm using, so I do not have password database.

I wonder if something is wrong or I have to put something else in it ... since the code does not work.

ps. I copied exactly as it is.

 $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "dados";
    //conexão ao banco de dados
    $conn = new mysqli($servername, $username, $password, $dbname);
    //consulta a tabela Artistas onde spotify_popularity é menor que 20 no total de 7 registros
    $consulta = "SELECT * FROM artists WHERE spotify_popularity < 20 limit 7";

    $result = $conn->query($consulta);

    $i=1;
    echo '<div id="container">';

    while($dados = $result->fetch_array()){
        echo "<div id='box-".$i."' class='box'>";
        echo "<img src=". $dados["imagem_small"]. ">";
        echo "<br>";
        echo "<span>". $dados["Name"]. "</span>"; 
        echo "</div>";
        $i++;
    }

    echo '</div>';
    
asked by anonymous 26.09.2017 / 21:51

2 answers

0

CSS:

  • Centralize the div container with width of 100%.
  • Arrange the divs class box side by side with margin of 10px

CSS

<style>
    #container {
       width: 100%;
       text-align: center;
    }

    .box {
      display: inline-block;
      margin: 10px 10px;
    }
</style>

PHP

PHP: with MySQLi

<?php

    $imagem_small="";
    $Name="";

    $servername = "localhost";
    $username = "USUARIO";
    $password = "SENHA";
    $dbname = "NOME_DB";
    //conexão ao banco de dados
    $conn = new mysqli($servername, $username, $password, $dbname);
    //consulta a tabela Artistas onde spotify_popularity é menor que 20 no total de 7 registros
    $consulta = "SELECT * FROM artists WHERE spotify_popularity < 20 limit 7";

    $result = $conn->query($consulta);

    $i=1;
    echo '<div id="container">';

    while($dados = $result->fetch_array()){
        echo "<div id='box-".$i."' class='box'>";
        echo "<img src=". $dados["imagem_small"]. ">";
        echo "<br>";
        echo "<span>". $dados["Name"]. "</span>"; 
        echo "</div>";
        $i++;
    }

    echo '</div>';
?>
  

The table name is not Artists as stated in the question and yes artists as found in link

    
27.09.2017 / 01:09
0

The right thing is to provide a code to help us help you. But here's an example of% how to do it.

<?php
    try {
       $pdo = new PDO('mysql:host=localhost;dbname= dados', 
       'root', '');
       $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
       echo 'ERROR: ' . $e->getMessage();
    }

    $sql = $pdo->prepare("SELECT * FROM Artists");
    $sql->execute();

    while($ln = $sql->fetchObject()){
       echo '<h1>'.$ln->Name.'</h1>';
       echo '<img src="'.$ln->imagem_small.'">';

    }

This will display the band name and image, and you can change the code to suit you better.

    
26.09.2017 / 21:58