Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. Error:
  2. Connected to temp at localhost successfully.
  3. Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs\temp\view.php on line 12
  4. ------------------------------------
  5.  
  6. dbconnect.php
  7.  
  8. <?php
  9. $host = 'localhost';
  10. $dbname = 'temp';
  11. $username = 'root';
  12. $password = '';
  13.  
  14. try {
  15. $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
  16. echo "Connected to $dbname at $host successfully.";
  17. $pdo = null;
  18. } catch (PDOException $pe) {
  19. die("Could not connect to the database $dbname :" . $pe->getMessage());
  20. }
  21. ------------------------------------
  22. view.php
  23. <!DOCTYPE html>
  24. <html>
  25. <head>
  26. <title></title>
  27. </head>
  28. <body>
  29. <?php
  30. require_once 'dbconnect.php';
  31.  
  32. $sql = 'select name,unit,price from items';
  33. //$q = $pdo->query($sql);
  34. $q = $pdo->prepare($sql); // -> ===== Line 12 =====
  35. $q->execute();
  36. $q->setFetchMode(PDO::FETCH_ASSOC);
  37.  
  38. ?>
  39. <div id="container">
  40. <h1>Employees</h1>
  41. <table class="table table-bordered table-condensed">
  42. <thead>
  43. <tr>
  44. <th>Name English</th>
  45. <th>Unit</th>
  46. <th>Price</th>
  47. </tr>
  48. </thead>
  49. <tbody>
  50. <?php while ($row = $q->fetch()): ?>
  51. <tr>
  52. <td><?php echo htmlspecialchars($row['name']) ?></td>
  53. <td><?php echo htmlspecialchars($row['unit']); ?></td>
  54. <td><?php echo htmlspecialchars($row['price']); ?></td>
  55. </tr>
  56. <?php endwhile; ?>
  57. </tbody>
  58. </table>
  59. </div>
  60. </body>
  61. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement