2016-06-19 92 views
-1

我新的PHP和我試圖「PHP代碼上傳數據庫表BLOB數據類型的圖像」 ...... 如上標題PDO越來越不確定的變量

<?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

+0

什麼是對的'blob.php'從你手上有這個地方 –

+0

19行? '$ strSQL->' –

+0

你的意思是'$ strSQL = insertBlob(...)'? –

回答

1

錯誤說明變量$strSQL未定義。

$this->pdo->prepare在您的功能中也沒有意義。

你在混合OOP和功能概念。

更新

我認爲connection.php是一流的,而且$strSQL是類的實例,insertBlob()應該是類定義不出來內。

所以在包括創建新實例,

$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");