PDO得到未定义的变量

时间:2016-06-19 18:56:53

标签: php insert blob

我是php的新手,我正在尝试使用blob数据类型" Php代码在数据库表中上传图像... 我在标题中提到了上述错误

<?php include 'connection.php';

 function insertBlob($filePath, $mime) 
  {
    $blob = fopen($filePath, 'rb');

    $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)";
    $stmt = $this->pdo->prepare($sql);

    $stmt->bindParam(':mime', $mime);
    $stmt->bindParam(':data', $blob, PDO::PARAM_LOB);

    return $stmt->execute();
 }

 $strSQL-> insertBlob('C:\Users\Vishal\Desktop\house.png',"image/png");

 if(mysqli_query($con, $strSQL)){
 echo "Records added successfully.";
 } else{
 echo "ERROR: Could not able to execute $strSQL. " . mysqli_error($con);
 }

 ?>

enter image description here

1 个答案:

答案 0 :(得分:1)

错误表示变量$strSQL未定义。

$this->pdo->prepare对你的功能也没有意义。

您正在将OOP与功能概念混合在一起。

<强>更新

我认为connection.php是类,$strSQL是该类的实例, insertBlob()应该在类定义中而不是

所以在include下,创建新实例,

$strSQL = new WhatEverClass()

class MyConnection {
     protected $pdo;

     public function __construct() {
        $this->pdo = ...
     }

     public function insertBlob($filePath, $mime) 
      {
        $blob = fopen($filePath, 'rb');

        $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)";
        $stmt = $this->pdo->prepare($sql);

        $stmt->bindParam(':mime', $mime);
        $stmt->bindParam(':data', $blob, PDO::PARAM_LOB);

        return $stmt->execute();
     }

}

    $strSQL = new MyConnection();
    $strSQL->insertBlob('C:\Users\Vishal\Desktop\house.png',"image/png");