0

I am trying to use PHP in two different parts of a page. But when I try to load the page I receive the error: undefined variable $conn. Why am I getting this error?

<tbody>
<?php
     $sql = "SELECT id, name, price FROM periperichicken";
     $result = mysqli_query($conn, $sql);
     if (mysqli_num_rows($result) > 0) {
         // output data of each row
         while($row = mysqli_fetch_assoc($result)) {
             echo "<tr>";
             echo "<td>" . $row["name"] . "</td>";
             echo "<td>£" . $row["price"] . "</td>";
             echo "<td><input type=\"text\" name=\"amount\" 
                 class=\"amount-type\"
                 placeholder=\"Amount\"/></td>";
             echo "<td><a href=\"\" class=\"add-cart\">Add to cart</a></td>";
             echo "</tr>";
         }
     }
     mysqli_close($conn);
?> 
</tbody>

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "pizza";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
?>
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
hasan
  • 55
  • 6
  • 1
    Take a moment to read through the [editing help](http://stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than other sites. The better your post looks, the easier it will be for users to help you. – gunr2171 Dec 17 '14 at 19:46

4 Answers4

1

You're defining and connecting to the database after you try to use it. Move the connection part above the query. If this is the only place that you will be using the connection the page then this is fine. But if other pages or sections of the page will use the connection you should look at placing this in a common file that is included at the top of every page that uses it.

                        <tbody>
                            <?php
                            $servername = "localhost";
                            $username = "root";
                            $password = "";
                            $dbname = "pizza";

                            // Create connection
                            $conn = mysqli_connect($servername, $username, $password, $dbname);
                            // Check connection
                            if (!$conn) {
                                die("Connection failed: " . mysqli_connect_error());
                            }

                            $sql = "SELECT id, name, price FROM periperichicken";
                            $result = mysqli_query($conn, $sql);

                            if (mysqli_num_rows($result) > 0) {
                                // output data of each row
                                while($row = mysqli_fetch_assoc($result)) {
                                    echo "<tr>";
                                    echo "<td>" . $row["name"] . "</td>";
                                    echo "<td>£" . $row["price"] . "</td>";
                                    echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
                                    echo "<td><a href=\"\" class=\"add-cart\">Add to cart</a></td>";
                                    echo "</tr>";
                                }
                            }

                            mysqli_close($conn);
                            ?> 
                        </tbody>
slapyo
  • 2,979
  • 1
  • 15
  • 24
1

You use $conn at the top of your script

$result = mysqli_query($conn, $sql);

Yet you define it further down

$conn = mysqli_connect($servername, $username, $password, $dbname);

You'll need to move that section up to the first before you can run any queries.

<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pizza";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

<tbody>
<?php
  $sql = "SELECT id, name, price FROM periperichicken";
  $result = mysqli_query($conn, $sql);

  if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
      echo "<tr>";
      echo "<td>" . $row["name"] . "</td>";
      echo "<td>£" . $row["price"] . "</td>";
      echo "<td><input type=\"text\" name=\"amount\" class=\"amount-type\" placeholder=\"Amount\"/></td>";
      echo "<td><a href=\"\" class=\"add-cart\">Add to cart</a></td>";
      echo "</tr>";
    }
  }

  mysqli_close($conn);
?> 
</tbody>
Robbert
  • 6,481
  • 5
  • 35
  • 61
0

I'd be pretty sure that you need to define $conn before you call it in the top part of the php code.

BigScar
  • 343
  • 3
  • 9
0

PHP is loaded and executed sequentially... You are defining $conn after using it. Try doing it before. I.e.: Move the second blob of PHP above the first one.

For more on PHP execution order, check out this question.

Community
  • 1
  • 1
djs
  • 56
  • 2