Search code examples
phpmysqlno-response

Browser doesn't show anything - SQL Query with PHP


I'm new to working with PHP and SQL and I need help with this.

<!doctype html>
<html lang="de">
  <head>
    <meta charset="utf-8">
    <link href="styles.css" rel="stylesheet">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Holzstern GmbH</title>
  </head>

  <body>
    <div class="left-ctn">
      <header>
        <nav>
          <ul class="navi">
            <li><a href="index.php" title="Holzstern GmbH - Home">Home</a></li>
            <li><a class="active" href="produkte.php" title="Holzstern GmbH - Produkte">Produkte</a></li>
            <li><a href="bestseller.php" title="Holzstern GmbH - Bestseller">Bestseller</a></li>
          </ul>
        </nav>
    </header>
    <h1>unsere Produkte</h1>
  </div>

 <div class="right-ctn">
    <a href="index.php" class="logo" title="Holzstern GmbH">Holzstern GmbH</a>
      <main>
        <?php
          $servername = "localhost";
          $username = "root";
          $password = "";
          $database = "schmuckshop";

          //Verbindung mit Datenbank herstellen
          $connection = new PDO("mysql:host=$servername;dbname=$database;charset=utf8", $username, $password);

          //Anfrage vorbereiten und ausführen
          $query = $connection->prepare("SELECT schmuck.name AS s_name, schmuck.Preis, lieferant.name AS l_name, schmuck.bild, schmuck.schmuckart, schmuck.edelmetall, schmuck.verfuegbarkeit FROM schmuck, lieferant, verkauft WHERE lieferant.l_id = verkauft.l_id and schmuck.s_id = verkauft.s_id ORDER BY schmuck.s_id");
          $query->execute();
          $query->setFetchMode(PDO::FETCH_ASSOC);

          //Ergebnis zeilenweise auslesen
          while($row = $query->fetch()){

              $name = $row['s_name'];
              $preis = $row['Preis'];
              $lieferant = $row['l_name'];
              $bild = $row['bild'];
              $schmuckart = $row['schmuckart'];
              $edelmetall = $row['edelmetall'];
              $verfuegbarkeit = $row['verfuegbarkeit'];

              echo"
                <article class='schmuck'>
                  <div class='ctn-left'>
                    <div class='textDiv'>
                      <p class='name'>$name</p>
                      <p class='schmuckart'><span>Art: </span>$schmuckart</p>
                      <p class='material'><span>Material: </span>$edelmetall</p>
                      <p class='verfuegbarkeit'><span>Auf Lager: </span>$verfuegbarkeit</p>
                      <p class='preis'>$preis &euro;</p>
                    </div>
                     <div class='imgDiv'>
                        <img class='image' src='images/spiele/$bild' alt='Bild'>
                    </div>
                  </div>
                  <div class='ctn-right'>
                    <p class='hersteller'>$lieferant</div>
                  </div>
                </article>
              ";

          }//Ende while-Schleife
        ?> 
       </main>
  </div>
  </body>
</html>

I've been working with this code on another website but now it doesn't seem to work. It seems like the connection with the database works but it won't show anything in the browser.

Could anybody look at the code to see if there's any mistakes? If you can't find any I might have to check the join again but I can't find any mistakes by now.

I'm really new to this and I would very appreciate any help or advice.


Solution

  • First of all check your php version. (To confirm PHP is installed, you can use php -v in terminal, if you are on a Linux system.)

    Secondly, search for php.ini file & search for display_error and error_reporting keywords and put below values for those parameters:

    display_error=On;
    error_reporting=E_ALL;
    

    Thirdly, restart your apache server (or xamp/wamp if you are using them).

    You'll certainly get a clue where to move ahead if php is installed in your system.