gpt4 book ai didi

php - fatal error 。在第 29 行的 C :\xampp\htdocs\CRUD\read. php 中对 null 调用成员函数 prepare()

转载 作者:行者123 更新时间:2023-11-30 23:45:07 25 4
gpt4 key购买 nike

<分区>

我一直在关注这个关于 PHP 中的 CRUD 的教程,但是我遇到了一个我未能拦截的错误

Fatal error. Call to member function prepare() on null in C:\xampp\htdocs\CRUD\read.php on line 29** and this error ion line 29 of my code is **$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();
}


?>

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com