Php 无法将类PDO的对象转换为字符串
标题中引用的错误(显然)发生在第33行。作为旁注,如果你对我优化这个的方法有任何建议,或者只是一般性的建议,我洗耳恭听。谢谢 代码如下:Php 无法将类PDO的对象转换为字符串,php,oop,Php,Oop,标题中引用的错误(显然)发生在第33行。作为旁注,如果你对我优化这个的方法有任何建议,或者只是一般性的建议,我洗耳恭听。谢谢 代码如下: <?php error_reporting(E_ALL); require('config.php'); $filename = htmlentities($_FILES['file']['name']); $tmpname = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size
<?php
error_reporting(E_ALL);
require('config.php');
$filename = htmlentities($_FILES['file']['name']);
$tmpname = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
$filetype = $_FILES['file']['type'];
$file = $_FILES['file'];
class connect {
public function dbConnect($host, $user, $pass, $dbname) {
try {
global $dbcon;
$dbcon = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch (PDOException $e) {
print $e->getMessage();
}
}
}
class upload Extends connect {
public function uploadFile($dbcon, $filename, $filesize, $filetype, $file) {
if ($filesize > 2000000) {
echo "File too large!";
}
elseif ($filesize <= 2000000) {
$stmt = $dbcon->prepare("INSERT INTO upload (name, type, size, content) VALUES (?, ?, ?, ?)");
$stmt->$dbcon->bindParam(1, $filename);
$stmt->$dbcon->bindParam(2, $filetype);
$stmt->$dbcon->bindParam(3, $filesize);
$stmt->$dbcon->bindParam(4, $file);
$stmt->$dbcon->execute();
$stmt->$dbcon->close();
echo "File uploaded!";
}
else {
echo "Unexpected error! Please try again!";
}
}
}
$con = new connect;
$con->dbConnect($host, $user, $pass, $dbname);
$exec = new upload;
$exec->uploadFile($dbcon, $filename, $filesize, $filetype, $file);
您应该使用:
$stmt->bindParam();
从$stmt->$dbcon->bindParam()
中删除$dbcon
,您应该使用:
$stmt->bindParam();
将$dbcon
从$stmt->$dbcon->bindParam()
第一个建议中删除,标记第33行是全局的
?为什么要全球化变量?!为什么要使用global$dbcon;第33行:$stmt->$dbcon->bindParam(1,$filename)@为什么不呢?你能告诉我为什么我不应该这样做吗?@DustinL:第一个建议,标记第33行是global
?为什么要全球化变量?!为什么要使用global$dbcon;第33行:$stmt->$dbcon->bindParam(1,$filename)@为什么不呢?你能告诉我为什么我不该那样做吗?@DustinL:太棒了!非常感谢。愚蠢的错误,太棒了!非常感谢。愚蠢的错误。