未捕获的错误:调用未定义函数bindValue()

时间:2019-05-09 15:23:07

标签: php

我正在尝试从数据库中获取数据,但出现此错误

  

致命错误:未捕获错误:调用C:\ xampp \ htdocs \ includes \ article.php:17中未定义的函数bindValue()堆栈跟踪:#0 C:\ xampp \ htdocs \ article.php(11):在第17行的C:\ xampp \ htdocs \ includes \ article.php中,引发article-> fetch_data('0')#1 {main}

C:\ xampp \ htdocs \ includes \ article.php

<?php
class Article {

    public  function fetch_all(){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM articles");
        $query->execute();

        return $query->fetchAll();
    }

    public function fetch_data($article_id){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ? ");
        $query = bindValue(1, $article_id);
        $query->execute();

        return $query->fetch();
    } 
}
?>

C:\ xampp \ htdocs \ article.php

<?php

include_once('includes/connection.php');
include_once('includes/article.php');

$article = new Article;


if (isset($_GET['id'])){
    $id = $_GET['id'];
    $data = $article->fetch_data($id);

    print_r($data);
} else {
    header('Location: index.php');
    exit();

}
?>

2 个答案:

答案 0 :(得分:2)

更改此:

$query = bindValue(1, $article_id);

对此

$query->bindValue(1, $article_id);

bindValue是PDOStatement的方法,不是单独的函数 https://www.php.net/manual/en/pdostatement.bindvalue.php

答案 1 :(得分:1)

#* @post /histog #* @png #* @serializer contentType list(type='image/tif') function(name,a1,mainname,xlabname,ylabname,col,colom){ browser() b <- list(a = paste0("C:/Users/SafaT/Documents/code/", ".png")) png(file=b, width=600, height=350) ras = paste0(a1, 1:11, ".tif") landsat <- stack(ras) landsatRGB <- landsat[[c(4,3,2)]] landsatFCC <- landsat[[c(5,4,3)]] VI <- function(img, k, i) { bk <- img[[k]] bi <- img[[i]] vi <- (bk - bi) / (bk + bi) return(vi) } ndvi <- VI(landsat, 5, 4) hist(ndvi, main = mainname, xlab = xlabname, ylab= ylabname, col = col, xlim = c(-0.5, 1), breaks = as.numeric(colom), xaxt = 'n') axis(side=1, at = seq(-0.5,1, 0.05), labels = seq(-0.5,1, 0.05)) dev.off() } 是PDOStatement对象的方法,由prepare调用返回。您想要这样的东西:

bindValue()

您还可以使用命名参数:

$query = $pdo->prepare('SELECT * FROM articles WHERE article_id = ?');
$query->bindValue(1, $article_id);
$query->execute();

此外,不要依赖全局变量,它破坏了面向对象编程的基本概念。而是将PDO连接对象作为参数传递给Article对象。这称为dependency injection

$query = $pdo->prepare('SELECT * FROM articles WHERE article_id = :article_id');
$query->bindValue('article_id', $article_id);
$query->execute();

然后在实例化文章时只需传递class Article { protected $pdo; public function __construct($pdo) { $this->pdo = $pdo; } public function fetch_all() { $query = $this->pdo->prepare("SELECT * FROM articles"); $query->execute(); return $query->fetchAll(); } } 作为参数:

$pdo