Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:通过从数据库读取记录来调用布尔值上的成员函数fetch()_Php_Mysql_Database Connection - Fatal编程技术网

Php 致命错误:通过从数据库读取记录来调用布尔值上的成员函数fetch()

Php 致命错误:通过从数据库读取记录来调用布尔值上的成员函数fetch(),php,mysql,database-connection,Php,Mysql,Database Connection,我有readOne函数。此函数将从类别表中仅读取一条记录,并为名称和描述变量赋值。看起来是这样的: public function readOne($id) { $stmt = $this->conn->prepare('SELECT name, description FROM categories WHERE id = '.$id)->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); $t

我有readOne函数。此函数将从类别表中仅读取一条记录,并为名称和描述变量赋值。看起来是这样的:

public function readOne($id) {
    $stmt = $this->conn->prepare('SELECT name, description FROM categories WHERE id = '.$id)->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    $this->name = $result['name'];
    $this->description = $result['description'];
}
我试着用这个来称呼它:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset( $_POST['read'], $_POST['id']) ) {

    $cat = new Category($conn);

    $id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );


    echo $cat->readOne($id);
}}
?>
<div>
<h3>readOne():</h3>
<form action="" method="post">
<label>Category id :</label>
<input type="text" name="id" id="id" required="required" placeholder="Please Enter Id"/><br /><br />
<input type="submit" value="read" name="read"/><br />
</div>
编辑:连接代码:

<?php

class Database {

public function getConnection() {
    $result = false;
    try {
        $result = new PDO('mysql:host=localhost;dbname=demo', 'root', '');
    } catch(PDOException $e) { }
    return $result;
}
}
$db = new Database();
$conn = $db->getConnection();
if (!$conn) {
die("Error connecting to the database");
}

?>

您正在为
$stmt
分配返回值
execute()
。您需要替换行
$stmt=$this->conn->prepare('SELECT name,description FROM categories WHERE id='。$id)->execute()带有:

$stmt = $this->conn->prepare('SELECT name, description FROM categories WHERE id = '.$id);
$stmt->execute();

查询执行代码需要进行如下更改:-

$stmt = $this->conn->prepare('SELECT name, description FROM categories WHERE id = "'.$id.'"'); 
$stmt->execute(); 
$result = $stmt->fetch(PDO::FETCH_ASSOC); 
$this->name = $result['name'];
将此代码用于您的问题您在接受答案后当前所问的内容(
但现在当我单击“阅读”按钮时,什么都没有发生?
):-

<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset( $_POST['read'], $_POST['id']) ) {

    $cat = new Category($conn);

    $id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );


    $_SESSION['description'] =  $cat->readOne($id);
}}
?>
<div>
<h3>readOne():</h3>
<form action="" method="post">
<label>Category id :</label>
<input type="text" name="id" id="id" required="required" placeholder="Please Enter Id"/><br /><br />
<input type="submit" value="read" name="read"/><br />
</div>
<div class= "description"><?php if(isset($_SESSION['description']) && !empty($_SESSION['description'])){ echo $_SESSION['description'];}?></div>

readOne():
类别id:




@Anant可以使用带有多个参数的
isset()
。另外,
execute()
返回一个布尔值,因此您不能执行您的建议。@MrCode您是对的。谢谢
<?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset( $_POST['read'], $_POST['id']) ) {

    $cat = new Category($conn);

    $id = filter_input ( INPUT_POST , 'id', FILTER_SANITIZE_NUMBER_INT );


    $_SESSION['description'] =  $cat->readOne($id);
}}
?>
<div>
<h3>readOne():</h3>
<form action="" method="post">
<label>Category id :</label>
<input type="text" name="id" id="id" required="required" placeholder="Please Enter Id"/><br /><br />
<input type="submit" value="read" name="read"/><br />
</div>
<div class= "description"><?php if(isset($_SESSION['description']) && !empty($_SESSION['description'])){ echo $_SESSION['description'];}?></div>