用PHP将JPEG插入mysql
我对通过PHP脚本将.jpg图像插入数据库有点问题。DB连接确实工作正常,查询在phpmyadmin中工作,所以我认为问题出在PHP代码中。这是我的db表:用PHP将JPEG插入mysql,php,mysql,image-processing,Php,Mysql,Image Processing,我对通过PHP脚本将.jpg图像插入数据库有点问题。DB连接确实工作正常,查询在phpmyadmin中工作,所以我认为问题出在PHP代码中。这是我的db表: CREATE TABLE images( imageid INT( 11 ) PRIMARY KEY AUTO_INCREMENT , noteid INT( 11 ) NOT NULL , image LONGBLOB NOT NULL , FOREIGN KEY ( noteid ) REFERENCES notes( noteid )
CREATE TABLE images(
imageid INT( 11 ) PRIMARY KEY AUTO_INCREMENT ,
noteid INT( 11 ) NOT NULL ,
image LONGBLOB NOT NULL ,
FOREIGN KEY ( noteid ) REFERENCES notes( noteid )
ON UPDATE CASCADE ON DELETE RESTRICT
);
还有测试脚本:
// connecting to db
$db = new DB_CONNECT();
$path = "uploads/1_image_003.jpg";
$noteid = 3;
if(file_exists($path)) {
$handle = fopen($path, "rb");
$image = fread($handle, filesize($path));
fclose($handle);
$img = base64_encode($image);
$sql ="INSERT INTO images(noteid, image) VALUES('$noteid', '$img')";
mysql_query($sql) or die('Bad Query');
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
} else {
echo "File not found\n";
}
该脚本返回“错误查询”。您有什么建议吗?有什么问题吗?或者您能告诉我另一种将.jpg图像存储到mysql的方法吗?替换
mysql_query($sql) or die('Bad Query');
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
与
您应该考虑使用或扩展而不是<代码> MySqLQueQuess()/<代码>,因为它已被弃用。
尝试用
注:mysql.*
函数,而应该使用or我认为您需要进一步调试它以确定问题 在代码之前添加以下内容:
error_reporting(-1);
ini_set('display_errors', 'On');
此外,根据以下建议,尝试更换:
mysql_query($sql) or die('Bad Query');
与
为什么要在数据库中保存真实图像?您可以将图像文件的名称保存到数据库中。为什么要在数据库中插入图像?相反,您应该将文件路径存储在db中。。。无论如何,请尝试用mysql_错误替换“坏查询”,并查看您收到的mssg错误是什么。“$noteid”不应在quotes@CodingAnt,没关系。引号也适用于
INT
error_reporting(-1);
ini_set('display_errors', 'On');
mysql_query($sql) or die('Bad Query');
mysql_query($sql) or die('Bad Query'. mysql_error());