我一直在关注PHP的CRUD这个教程,但是我遇到了一个错误,我没有拦截
致命错误。在第29行的C:\ xampp \ htdocs \ CRUD \ read.php中调用成员函数prepare(),并且我的代码的错误离线29是** $ stmt = $ conn-> prepare($查询);
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>${$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();
}
?>
答案 0 :(得分:-1)
似乎您的连接失败或包括database.php失败
答案 1 :(得分:-1)
在您的database.php文件中找不到一个文件:
您必须添加“ return $ conn;”。在尝试块上
更正如下:-
<?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
return $conn;
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " .$e->getMessage();
}
?>