为什么我的哈希值总是全 0?
这是我的函数:
function process_image($path) {
global $mysqli;
list($width,$height) = getimagesize($path);
$hash = md5_file($path,true);
$pic = $mysqli->prepare('INSERT INTO pictures () VALUES ()');
$pic->execute();
$pic_id = $pic->insert_id;
$size = $mysqli->prepare("INSERT INTO picture_sizes (filename, type, picture_id, hash, width, height) VALUES (?,'FULL',?,?,?,?)");
$size->bind_param('sibii',$path,$pic_id,$hash,$width,$height);
if(!$size->execute()) {
echo $size->error.'<br/>';
}
}
“哈希”字段在我的数据库中始终显示为 0。我已将其设置为 BINARY(16)
。我猜我使用 $size->bind_param
不正确,但我不知道如何正确执行。 $hash
是二进制的,所以我应该使用 b
,不是吗?
Here's my function:
function process_image($path) {
global $mysqli;
list($width,$height) = getimagesize($path);
$hash = md5_file($path,true);
$pic = $mysqli->prepare('INSERT INTO pictures () VALUES ()');
$pic->execute();
$pic_id = $pic->insert_id;
$size = $mysqli->prepare("INSERT INTO picture_sizes (filename, type, picture_id, hash, width, height) VALUES (?,'FULL',?,?,?,?)");
$size->bind_param('sibii',$path,$pic_id,$hash,$width,$height);
if(!$size->execute()) {
echo $size->error.'<br/>';
}
}
The 'hash' field always shows up as 0s in my database. I've got it set to BINARY(16)
. I'm guessing I'm using $size->bind_param
incorrectly, but I can't figure out how to do it properly. $hash
is binary, so I should be using b
with it, no?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果出现错误,md5_file 返回false
,存储到数据库时会转换为0
。使用===
运算符检查函数是否返回 false。因为哈希是字符串,所以可以将参数设为
s
类型(字符串) 。In case of an error, md5_file returnsfalse
, which is converted to0
when stored in the database. Check if the function returns false using the===
operator.Because the hash is a string, you can make the parameter of the type
s
(string).您可能需要查看绑定参数,看看是否必须放入占位符来解释“FULL”。插入语句设置为将
type
放在 picture_id 之前,但在您的 bind_param 中没有对type
的引用。因此,MySQL 可能正在尝试使用picture_id
填充hash
。You may have to look at the bind param and see if you have to put in a place holder to account for 'FULL'. The insert statement is set to put
type
before picture_id, but in your bind_param you have no reference totype
. So it may be that MySQL is trying to populatehash
withpicture_id
.