View database data from

Asked

Viewed 2,026 times

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

Call table : Artists

where I have the information :

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

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

would look like this site http://pipocaplayfm.com/site/

That code is what I’m using to pull the content. localhost I am using, so do not have password the database.

I want to know if there is something wrong or I have to put something else ... since the code does not work.

ps. I copied exactly like this one .

 $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>';

2 answers

0

What is right is you provide a code to help us help you. But follow an example basico how to do.

<?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.'">';

    }

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

  • i put no code pq I have no idea how to do this. with this your code what more I need to connect to my bank ?

  • @jhonatansantos I added the connection to the bank

  • error appeared ERROR: SQLSTATE[HY000] [1049] Unknown database ' data'

  • You have a database created called dados?

  • yes yes i - https://snag.gy/6eBbAK.jpg

0


CSS:

  • Centralizes the div container with a width of 100%.
  • Features the Divs class box side by side with 10px margin

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 name of the table is not Artists as stated in the question and yes artists as set out in https://snag.gy/6eBbAK.jpg

  • I copied it and pasted it into a php page ...nothing works. I have to do something else ?

  • @jhonatansantos, if the example I posted, whose code is exactly the answer is working and for you is not, something out of our reach to tell you. Take a look at this post https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5484#5484

  • I just copy the code and paste - then put the bank information ...in the password case and etc ???

  • @jhonatansantos the name of your table in the bank is artists with the minuscule

  • I added on the question how did your code look, but it just shows the code and not the content. my question is ...if I have to do something else, create a page to accomplish something. 'cause I did it : I created a page and pasted the code.

  • Put the script between php tags? the file name has extension . php ?

  • how my page appears https://snag.gy/atOhdq.jpg as is my page code and extension https://snag.gy/TQDORw.jpg

  • @jhonatansantos I’m working on an online server, locally it’s not my thing!

  • I’ll test it tomorrow online and pass you the feedback .

  • I tested online check the link http://pipocaplayfm.com/teste/bandas.php not working correctly

  • You can do it in the code you suggested above just so I understand ? I’m layman on this guy.

Show 6 more comments

Browser other questions tagged

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