简体   繁体   English

PDO:绑定问题,所有查询返回false

[英]PDO: Problems with binding, all queries return false

I cannot for the life of me get binding to work with PDO queries, they always return false. 我一生都无法绑定到PDO查询,它们总是返回false。

On this example, it checks that the value of a field is between 2 other values. 在此示例中,它检查字段的值是否在其他2个值之间。

This works: 这有效:

$query = $db->query("SELECT * FROM table WHERE field1 > '$start' AND field1 < '$finish'");

This doesn't: 这不是:

$query = $db->query("SELECT * FROM table WHERE field1 > :start AND field1 < :finish");
$query->bindParam(":start", $start);
$query->bindParam(":finish", $finish);

UPDATE: The above query now works thanks to the help. 更新:由于有了帮助,以上查询现在可以使用。 The following still doesn't. 以下内容仍然没有。

I have been trawling through various PDO posts on here but I have not found a solution, and I don't know what else to try. 我一直在这里浏览各种PDO帖子,但是我没有找到解决方案,而且我不知道还能尝试什么。

UPDATE2: Okay, it seems it is not finding $db and therefore not connecting and returning false. UPDATE2:好的,似乎找不到$ db,因此没有连接并返回false。 The $db connection line is in a connect.php file that is required on all main pages. $ db连接行位于所有主页上所需的connect.php文件中。 The content on those pages is called by a function that then includes the relevant file/page. 这些页面上的内容由一个函数调用,该函数随后包含相关的文件/页面。 Because PDO does not work by itself in functions, is it losing the $db through the function to include the file containing the query? 由于PDO本身不能在函数中起作用,是否会通过函数丢失$ db来包含包含查询的文件? I may not have explained myself clearly enough. 我可能对自己的解释不够清楚。

Basically, example function in functions.php: 基本上,在functions.php中的示例函数:

function getRegistration() {
    include("registration.php");
}

main.php main.php

require_once("connect.php");
require_once("functions.php");

getRegistration();

registration.php contains: registration.php包含:

$sql = $db->prepare("INSERT INTO tempus_members(username, email, password, activation_code, registration_date, registered_ip, name) VALUES(:username, :email, :password, :activation_code, :registration_date, :registered_ip, :name)");
$sql->bindParam(":username", $username);
$sql->bindParam(":email", $email);
$sql->bindParam(":password", $hash);
$sql->bindParam(":activation_code", $activation_code);
$sql->bindParam(":registration_date", $registration_date);
$sql->bindParam(":registered_ip", $registered_ip);
$sql->bindParam(":name", $name);
$sql->execute();

Is it losing the $db variable through the function to include the page? 是否通过包含页面的函数丢失了$ db变量? If so, how do I carry $db through all functions? 如果是这样,我如何通过所有功能携带$ db?

Try: 尝试:

$stmt = $db->prepare("SELECT * FROM table WHERE field1 > :start AND field1 < :finish");
$stmt->bindParam(":start", $start);
$stmt->bindParam(":finish", $finish);
$stmt->execute();

You were using PDO::query instead of PDO::prepare . 您使用的是PDO :: query而不是PDO :: prepare

As for the other query, what errors are you getting back? 至于其他查询,您又得到了什么错误? Try the following code and see if any errors are spit out onto the page: 尝试下面的代码,看看是否有任何错误吐出到页面上:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try{
    $sql = $db->prepare("INSERT INTO tempus_members(username, email, password, activation_code, registration_date, registered_ip, name) VALUES(:username, :email, :password, :activation_code, :registration_date, :registered_ip, :name)");

    $sql->bindParam(":username", $username);
    $sql->bindParam(":email", $email);
    $sql->bindParam(":password", $hash);
    $sql->bindParam(":activation_code", $activation_code);
    $sql->bindParam(":registration_date", $registration_date);
    $sql->bindParam(":registered_ip", $registered_ip);
    $sql->bindParam(":name", $name);

    $sql->execute();

}
catch(PDOException $e){
    echo $e->getMessage();
}

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

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