2017-02-16 50 views
1

我一直在使用php在CRUD中学习本教程,但遇到了一个错误,我没有拦截致命错误。在第29行调用成员函数prepare()null:C: xampp htdocs CRUD read.php

致命错误。调用第29行**的C:\ xampp \ htdocs \ CRUD \ read.php中的null成员函数prepare(),并且我的代码的这个错误离子线29是** $ stmt = $ conn-> prepare($ query );

read.php文件是这样的

<!DOCTYPE html> 
<html> 
<head> 
    <title>PDO - Read Records - -PHP CRUD Tutorial</title> 

    <!--Bootstrap--> 
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css"> 

    <script src="bootstrap/js/bootstrap.min.js"></script> 
    </head> <body> 
    <div class="container"> 
     <div class="page-header"> 
      <h1>Read Products</h1> 
     </div> 
     <!--Dynamic content will go here--> 

     <?php 

     // include database connection 
     include_once 'config/database.php'; 

     // select all data 
     $query = "SELECT id, name, description, price FROM products ORDER BY id DESC"; 

     // prepare query for execution 
     $stmt = $conn->prepare($query); 

     // execute the query 
     $stmt->execute(); 

     // this how to get number of rows returned 
     $num = $stmt->rowCount(); 

     // link to create record form 
     echo "<a href='create.php' class='btn btn-primary m-b-1em'>Create New Product</a>"; 

     // check if more than 0 records found 
     if($num > 0) { 
      echo "<table class='table table-hover table-responsive table-bordered'>"; // start table 

      // creating our table heading 
      echo "<tr>"; 
      echo "<th>ID</th>"; 
      echo "<th>Name</th>"; 
      echo "<th>Description</th>"; 
      echo "<th>Price</th>"; 
      echo "<th>Action</th>"; 
      echo "</tr>"; 

      // retrieve our table contents 
      //fetch() is faster than fetchAll() 

      while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
       // extract row 
       // this will make $row['firstname'] to 
       // just $firstname only 
       extract($row); 

       // creating new tablerow per record 
       echo "<tr>"; 
        echo "<td>{$id}</td>"; 
        echo "<td>{$name}</td>"; 
        echo "<td>{$description}</td>"; 
        echo "<td>&#36;{$price}</td>"; 
        echo "<td>"; 

       // read one record 
       echo "<a href='read_one.php?id={$id}'class=''btn btn-info m-r-1em'>Read</a>"; 

       // we will use this link to the next part of the post 
       echo "<a href='update.php?id={$id}' class='btn btn-primary m-r-1em'>Edit</a>"; 

       // we will use this link to the next part of the post 
       echo "<a href='#' onClick='delete_user({$id});' class='btn btn-danger'>Delete</a>"; 

       echo "</td>"; 
      echo "</tr>"; 
      } 
      //end table 
     echo "</table>"; 

     } 

     // if no records found 
     else{ 
      echo "<div class='alert alert-danger'>No records found.</div>"; 
     } 
     ?> 

    </div><!--end of container--> 

    <!--Jquery (necessary for bootstrap's javascript plugin)--> 
    <script src="jquery-ui-bootstrap-jquery-ui-bootstrap-71f2e47/js/jquery-1.8.3.min.js"> 
</script> 
</body> 
</html> 

database.php文件:

<?php 
    //variables used to connect to the database 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "1phpbeginnercrudlevel1"; 

    //create a connection using the PDO extension 
    try{ 
     $conn = new PDO("mysql:host=$servername;dbname=1phpbeginnercrudlevel1",$username,$password); 

     //set the PDO error mode to exception 
     echo "Connected successfully"; 
    } 
    catch(PDOException $e) 
    { 
     echo "Connection failed: " .$e->getMessage(); 
    } 


    ?> 
+3

http://php.net/ manual/en/pdo.error-handling.php --- http://php.net/manual/en/function.error-reporting.php –

+2

尊敬的Fr ED!顺便说一下,也许你的连接失败。查看Fred链接并在try/catch之前声明一个$ conn,并让它存在。 – Grork

+0

我真的尝试过它,但仍然给我同样的错误....这行代码可能是错误的$ stmt = $ conn-> prepare($ query); –

回答

-1

好像你的连接失败或包括database.php中是失败

+0

嘿谢谢,我已经想通了,它没有访问数据库文件,所以我决定直接将它包含到我的read.php文件中,而不是使用include命令......但是有什么问题吗?我如何解决这个问题,以便我可以使用包含命令 –

+0

你应该检查连接。所以如果($ conn)然后继续操作。否则显示一些错误消息 –

相关问题