-2

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.

Munim Munna
  • 15,455
  • 6
  • 22
  • 52
E.Digital
  • 3
  • 2
  • 1
    Wont show anything? Do you mean, nothing in the browser, no html, zilch? Or your html is missing a block of what you thought should show up there? – IncredibleHat Jun 06 '18 at 17:25
  • 2
    If your page is blank and not showing errors, then you need to enable error reporting in PHP and MySQL. (You can _not_ develop in PHP without error reporting - otherwise, you are flying completely blind, and all we are doing is guessing). See this question / answers: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display - also, it's my strong guess that it's the PDO connection (you say it seems fine, but don't explain why) - so I'd additionally recommend you do some try/catch like shown here: https://stackoverflow.com/a/6263868/870729 – random_user_name Jun 06 '18 at 17:25
  • Also, best db name ever ;) – random_user_name Jun 06 '18 at 17:29
  • "It seems like the connection with the database works" What debugging have you done to confirm this? – Patrick Q Jun 06 '18 at 17:31
  • Everything works fine, only the problem is with your query, run a simple `Select * from a_table` query that should work and then debug your current query. As mentioned above enable error reporting in PHP. – webDev Jun 06 '18 at 17:32
  • And the HTML is empty inside the
    tag.
    – E.Digital Jun 06 '18 at 17:36
  • Also I can connect with the database in another index.php. – E.Digital Jun 06 '18 at 17:38
  • Please edit your question to add relevant code and information. Do not put code in comments. As you can see, it is quite hard to read. – Patrick Q Jun 06 '18 at 17:57

1 Answers1

0

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.

Striezel
  • 3,274
  • 7
  • 19
  • 33
VPS
  • 36
  • 5
  • 1
    This is a comment, it does not attempt to actually answer the question. When you have enough reputation, you will have the ability to post comments. In the meantime, please do not post comments as answers, as the will likely be downvoted, thereby prolonging the time it takes you to build up your reputation. – Patrick Q Jun 06 '18 at 17:50