0

I am currently trying to output a the results of a MySQL table in PHP. I have a general understanding of web based programming but not enough to debug my code. I know the SQL is good, and the database is linked to my site it's just a matter of making it post to a table. I will post the code and would appreciate some help:

<?php 

        $sql = "SELECT player_name AS 'Name',
        position AS 'Position',
        team AS 'Team',
        opp AS 'Opponent'
        FROM `dbname`
        WHERE position = 'QB'";

        $stmt = $db->query($sql);

        if($stmt-> num_rows > 0) {
            echo "<table class='table'>";
            echo "<thead class='thead-inverse'>";
            echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
            echo "</thead>";
            echo "<tbody>";
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                echo "<tr><td>";
                echo $row['Name'];
                echo "</td><td>";
                echo $row['Position'];
                echo "</td><td>";
                echo $row['Team'];
                echo "</td><td>";
                echo $row['Opponent'];
                echo "</td></tr>";

         }
         echo "</tbody>";
         echo "</table>";
      }
      else {
          echo "No Results";
      }

All that I get from this is a no results output.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
J. Young
  • 11
  • 1

1 Answers1

0

You forgot to close the query with a double quote.

<?php 

    $sql = "SELECT player_name AS 'Name',
    position AS 'Position',
    team AS 'Team',
    opp AS 'Opponent'
    FROM `dbname`
    WHERE position = 'QB'";

    $stmt = $db->query($sql);
    $stmt -> execute();

    if($stmt-> num_rows > 0) {
        echo "<table class='table'>";
        echo "<thead class='thead-inverse'>";
        echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
        echo "</thead>";
        echo "<tbody>";
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "<tr><td>";
            echo $row['Name'];
            echo "</td><td>";
            echo $row['Position'];
            echo "</td><td>";
            echo $row['Team'];
            echo "</td><td>";
            echo $row['Opponent'];
            echo "</td></tr>";

     }
     echo "</tbody>";
     echo "</table>";
  }
  else {
      echo "No Results";
  }

Add $stmt -> execute();

You can also use $stmt -> store_result(); after the execute part

Irvin
  • 830
  • 5
  • 13
  • I shortened the query to make it easier to type, that was a typo. in the actual code I did close the query with a quote. The issue is not in the query but in putting the results into the table. – J. Young Oct 06 '16 at 00:02