Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Error:
- Connected to temp at localhost successfully.
- Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\temp\view.php on line 12
- ------------------------------------
- dbconnect.php
- <?php
- $host = 'localhost';
- $dbname = 'temp';
- $username = 'root';
- $password = '';
- try {
- $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
- echo "Connected to $dbname at $host successfully.";
- $pdo = null;
- } catch (PDOException $pe) {
- die("Could not connect to the database $dbname :" . $pe->getMessage());
- }
- ------------------------------------
- view.php
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <?php
- require_once 'dbconnect.php';
- $sql = 'select name,unit,price from items';
- //$q = $pdo->query($sql);
- $q = $pdo->prepare($sql); // -> ===== Line 12 =====
- $q->execute();
- $q->setFetchMode(PDO::FETCH_ASSOC);
- ?>
- <div id="container">
- <h1>Employees</h1>
- <table class="table table-bordered table-condensed">
- <thead>
- <tr>
- <th>Name English</th>
- <th>Unit</th>
- <th>Price</th>
- </tr>
- </thead>
- <tbody>
- <?php while ($row = $q->fetch()): ?>
- <tr>
- <td><?php echo htmlspecialchars($row['name']) ?></td>
- <td><?php echo htmlspecialchars($row['unit']); ?></td>
- <td><?php echo htmlspecialchars($row['price']); ?></td>
- </tr>
- <?php endwhile; ?>
- </tbody>
- </table>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement