简体   繁体   English

PHP:PDOStatement简单的MySQL Select不起作用

[英]PHP: PDOStatement simple MySQL Select doesn't work

I have the following PHP code doing a very simple select into a table. 我有以下PHP代码在表中进行非常简单的选择。

$statement = $db->prepare("SELECT * FROM account WHERE fbid = :fbid");
$statement->bindParam(":fbid",$uid, PDO::PARAM_STR,45);
$out = $statement->execute();
print_r($out) // 1;
//$out = $statement->execute(array(':fbid' => $uid)); // also doesn't work
$row = $statement->fetch();

$out is true (success) yet $row is null. $out为真(成功),但$row为null。

EDIT: 编辑:

$statement->debugDumpParams();

Outputs 产出

SQL: [40] SELECT * FROM account WHERE fbid = :fbid Params: 1 Key: Name: [5] :fbid paramno=-1 name=[5] ":fbid" is_param=1 param_type=2

If I modify the code as follows: 如果我将代码修改如下:

$statement = $db->prepare("SELECT * FROM account WHERE fbid = $uid");
$out = $statement->execute();
$row = $statement->fetch();

$row contains the record I'm expecting. $row包含我期望的记录。

I'm at a loss. 我很茫然。 I'm using the PDO::prepare(), bindParams() etc to protect against SQL Injection (maybe I'm mistaken on that). 我正在使用PDO :: prepare(),bindParams()等来防止SQL注入(也许我误会了)。

EDIT: In my example, $uid is a numerical string (ie a string containing only numbers). 编辑:在我的示例中,$ uid是一个数字字符串(即仅包含数字的字符串)。 In the database, the column type is VARCHAR(45) 在数据库中,列类型为VARCHAR(45)

EDIT: 编辑:

If I change the database type from VARCHAR(45) to BIGINT, both queries work. 如果我将数据库类型从VARCHAR(45)更改为BIGINT,则两个查询均有效。 If I change the type in the database type back to VARCHAR(45) again, it works. 如果我将数据库类型的类型再次更改回VARCHAR(45),则可以正常工作。 So what gives? 那有什么呢?

Please halp. 请暂停。

You need to check your fbid value. 您需要检查您的fbid值。 it should be always string if its integer value is greater than 2^32 (unsigned), simply cast by (string)$uid is not work, and sprintf("%.0f",...) will only works when integer value less than 2^52, because on 32-bit OS when a number is greater than 2^31(32 unsigned) PHP will assume it is double type and default precise is only 14 decimal but fbid is 20. 如果其整数值大于2 ^ 32(无符号),则应始终为字符串,仅由(string)$ uid强制转换不起作用,并且sprintf(“%。0f”,...)仅在整数值时有效小于2 ^ 52,因为在32位OS上,当数字大于2 ^ 31(32无符号)时,PHP将假定它为双精度类型,默认精确度仅为14个十进制,而fbid为20。

You have to keep fbid in string contains only [0-9] in PHP, doesn't matter it is stored as BIGINT or VARCHAR in MySQL, MySQL accepts only string sql statement and always returns result in string format. 您必须将fbid保留在PHP中仅包含[0-9]的字符串中,无论它在MySQL中存储为BIGINT还是VARCHAR,MySQL都仅接受字符串sql语句,并且始终以字符串格式返回结果。

$mi = new mysqli("localhost", "root", "xxx", "test");
$uid = "12379739851403943597";   // Works
//$uid = 12379739851403943597;   // never Works
//$uid = (string) 12379739851403943597;   // get "1.2379739851404E+19" wrong string !
//$suid = sprintf("%.0f", $uid);          // get "12379739851403943936" lost precise

$stmt = $mi->prepare("select * from bitest where id = ?");
$stmt->bind_param('s', $uid);

$stmt->execute();
$stmt->bind_result($id, $name);

$stmt->store_result();
print "numrow: " . $stmt->num_rows . " - \n";
$stmt->fetch();
print "$id - $name \n";
$stmt->free_result();

$stmt->close();


$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'xxx');
$sql = "select * from bitest where id = ?";

$sth = $pdo->prepare($sql);
$sth->bindParam(1, $uid, PDO::PARAM_STR);

$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_ASSOC));

已经有一段时间了...尝试传递一个散列来代替执行

$statement->execute(array( 'fbid' => $uid ));

Try dropping the extra parameter, 尝试删除多余的参数,

$statement->bindParam (":fbid", $uid, PDO::PARAM_STR);

(edit) Are you 100% positive there is no extra whitespace surrounding the UID? (编辑)您是否100%肯定UID周围没有多余的空格? Test with trim() and pass by value: 使用trim()测试并按值传递:

$statement->bindValue (":fbid", trim($uid), PDO::PARAM_STR);

Maybe try PDO::PARAM_INT 也许尝试PDO :: PARAM_INT

Aside from that, keep in mind bindParam() takes the variable as a reference. 除此之外,请记住bindParam()将变量作为参考。 Maybe your demo code doesn't show you changing the value of that variable before execute() is called. 也许您的演示代码未显示在调用execute()之前更改该变量的值。 See bindValue() if needed. 如果需要,请参见bindValue()。

I think there may be an issue with your PDO installation. 我认为您的PDO安装可能存在问题。

$uid = 552192373; // my facebook uid for testing
$statement = $db->prepare("SELECT * FROM users WHERE facebook_uid = :fbid");
$statement->bindParam(":fbid",$uid, PDO::PARAM_STR,45);
$out = $statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo '<pre>';
print_r($row);
echo '</pre>';

returns: 返回:

Array
(
    [id] => 1
    [facebook_name] => Jason Boehm
    [facebook_uid] => 552192373
)

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

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