简体   繁体   English

PDO和动态请求不起作用

[英]PDO and dynamic request doesn't work

I have the following code : 我有以下代码:

$sth = $dbh->prepare('SELECT COUNT(*) as size FROM `users` WHERE uid  = :uidVal');
$sth->bindValue(':uidVal', $_POST['uid'], PDO::PARAM_INT); 
// $_POST['uid'] == 2147483647
$sth->execute();
$size = $sth->fetchAll();

print_r($size);

The print_r() return : print_r()返回:

Array ( [0] => Array ( [size] => 0 [0] => 0 ) ) 数组([0] =>数组([size] => 0 [0] => 0))

But this is my data base (mysql) 但这是我的数据库(mysql)

在此处输入图片说明

I don't understand why my COUNT(*) return 0, normally it should return 3? 我不明白为什么我的COUNT(*)返回0,通常应该返回3?

EDIT (based on the comments) 编辑(基于评论)

This is my code after the select statement : 这是我在select语句之后的代码:

    $stmt = $dbh->prepare("INSERT INTO `users` (uid, uname) VALUES (?, ?)");
    $stmt->bindParam(1, $uid);
    $stmt->bindParam(2, $name);

    // insertion d'une ligne
    $name = $_POST['uname'];
    $uid = $_POST['uid'];
    $stmt->execute();

Why into my data base the uid value is 2147483647 while the uid value in my $_POST is 10152434954117198 为什么在我的数据库中,uid值为2147483647而我的$ _POST中的uid值为10152434954117198

The uid value is to big for my data base configuration : 对于我的数据库配置,uid值很大:

在此处输入图片说明

10152434954117198 is a long not an integer 10152434954117198是long而不是整数

Try this code. 试试这个代码。

$sth = $dbh->prepare('SELECT COUNT(*) as size FROM `users` WHERE uid  = ?');
$sth->bindValue(1, $_POST['uid']); 
// $_POST['uid'] == 2147483647
$sth->execute();
$size = $sth->fetchAll();

print_r($size);

OR you can try this too 或者你也可以尝试

$sth = $dbh->prepare('SELECT COUNT(*) as size FROM `users` WHERE uid  = :uidVal'); 
// $_POST['uid'] == 2147483647
$sth->execute([
    "uidVal" => $_POST['uid']
]);
$size = $sth->fetchAll();

print_r($size);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM