简体   繁体   English

使用PHP / PDO设置NULL值

[英]Using PHP/PDO to set a NULL value

Using PHP/PDO I'm trying to set a MySQL (mysql-5.0.96) variable named “flt_status” whose default is set to 'NULL' and which is defined as INT(5), nullable, to NULL. 我正在尝试使用PHP / PDO将一个名为“ flt_status”的MySQL(mysql-5.0.96)变量设置为默认值“ NULL”,并将其定义为INT(5)(可为空)。

There are a number of threads here on StackOverFlow (11692773, 1391777) that cover this topic but none of them seems to work for me. 在StackOverFlow(11692773,1391777)上有很多线程可以解决这个问题,但是似乎没有一个对我有用。

My (very abbreviated) code looks like this; 我的代码(非常简短)如下所示;

$vs = “:ip, flt_status, sunrise”;

$match = array( 
‘:ip’=>”$ip”, 
‘:flt_status’=>”null, PDO::PARAM_INT”,
’:sunrise’=>”$sunrise”
);

$vars = “ip, flt_status, sunrise”;

$sql = "INSERT INTO skeds ( $vars ) VALUES ( $vs )";

$query = $db_found->prepare( $sql );

$query->execute(  $match );

I've tried a number of techniques outlined in the above discussions and others I've found using Google but every time the value of flt_status comes out as zero (0). 我已经尝试了上述讨论中概述的许多技术以及使用Google发现的其他技术,但是每当flt_status的值显示为零(0)时。 I've tried using both PDO::PARAM_NULL and PDO::PARAM_INT. 我试过同时使用PDO :: PARAM_NULL和PDO :: PARAM_INT。

I've included the IP and SUNRISE variables in this example so I can better understand any example the more experienced PHP/PDO programmers out there give me. 我在此示例中包括了IP和SUNRISE变量,因此我可以更好地理解经验丰富的PHP / PDO程序员给我的任何示例。

Can someone show me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

Thanks in advance for any assistance you can offer. 在此先感谢您提供的任何帮助。

You are doing several things wrong. 您做错了几件事。

$vs = “:ip, flt_status, sunrise”;

First, you're apparently using smart quotes instead of straight quotes. 首先,您显然在使用智能引号而不是直引号。 Code needs straight quotes. 代码需要直接引号。

Next, you put a : prefix before ip but you missed that prefix before the other two named parameters. 接下来,您在ip前面放置一个:前缀,但是在其他两个命名参数之前错过了该前缀。 You need a : before each one . 您需要一个:每个之前。

$match = array( 
‘:ip’=>”$ip”, 
‘:flt_status’=>”null, PDO::PARAM_INT”,
’:sunrise’=>”$sunrise”
);

Next, you put null inside a quoted string. 接下来,将null放在带引号的字符串中。 That makes it a string literal containing the word 'null' , not a true null. 这使其成为包含单词'null'的字符串文字,而不是真正的null。 These two concepts are different, like the difference between a chicken and a piece of paper with the word "chicken" written on it. 这两个概念是不同的,例如鸡肉和一张纸上写着“鸡肉”一词的区别。

Also, you don't need to put quotes around the variables. 另外,您不需要在变量周围加上引号。

Also, for what it's worth, the : prefix on the array keys of your array are optional. 同样,值得的是,数组的数组键上的:前缀是可选的。 In early versions of PDO, they were mandatory, but now they're not. 在早期版本的PDO中,它们是强制性的,但现在不是强制性的。 It does no harm to keep the colon prefix, I just wanted to let you know because it could make it easier in the future to prepare arrays of parameters from another associative array, like $_POST. 保留冒号前缀没有什么害处,我只是想告诉您,因为它可以使将来更容易从另一个关联数组(如$ _POST)中准备参数数组。

$vars = “ip, flt_status, sunrise”;

This is fine except for the continued use of smart quotes. 除了可以继续使用智能引号之外,这还不错。

$sql = "INSERT INTO skeds ( $vars ) VALUES ( $vs )";

Here's where you will get into trouble with your $vs because it contains only one parameter placeholder, followed by two plain column names. 在这里,您将遇到$vs麻烦,因为它只包含一个参数占位符,后跟两个普通列名。 Passing column names in the VALUES clause is not illegal in SQL, but it makes no sense to do it. 在VALUES子句中传递列名在SQL中不是非法的,但是这样做是没有意义的。

$query = $db_found->prepare( $sql );
$query->execute(  $match );

Okay, except that you are not checking for errors. 好的,除了不检查错误。 Unless you have enabled PDO's attribute for throwing exceptions, you need to check the return status of prepare() and execute() because they return false if there's any error. 除非您已启用PDO的引发异常的属性,否则您需要检查prepare()execute()的返回状态,因为如果有任何错误,它们将返回false This continues to be one of the most common mistakes among PHP developers! 这仍然是PHP开发人员中最常见的错误之一!


Here's how I would write this code. 这是我编写此代码的方式。

As you connect to PDO, enable exceptions. 连接到PDO时,请启用例外。 See http://php.net/manual/en/pdo.error-handling.php 参见http://php.net/manual/en/pdo.error-handling.php

$db_found = new PDO(...);
$db_found->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

Then the code for this routine: 然后是该例程的代码:

$placeholders = ":ip, :flt_status, :sunrise";

$values = array( 
    'ip'         => $ip, 
    'flt_status' => null,
    'sunrise'    => $sunrise
);

$columns = "ip, flt_status, sunrise";

$sql = "INSERT INTO skeds ($columns) VALUES ($placeholders)";

$stmt = $db_found->prepare($sql);

$stmt->execute($values);

You have few mistakes in your code, try to adjust it like this: 您的代码中有几个错误,请尝试像这样调整它:

$sql = "INSERT INTO skeds ( ip, flt_status, sunrise ) VALUES ( :ip, :flt_status, :sunrise )";
$query = $db_found->prepare( $sql );
$query->execute( array(':ip'=> $ip, 
                       ':flt_status'=>null,
                       ':sunrise'=>$sunrise
));

When you say 当你说

$match = array( 
‘:ip’=>”$ip”, 
‘:flt_status’=>”null, PDO::PARAM_INT”,
’:sunrise’=>”$sunrise”
);

it's going to use that actual string "null, PDO::PARAM_INT". 它将使用实际的字符串“ null,PDO :: PARAM_INT”。 If you want a NULL, bind the placeholder to the PHP value null . 如果要为NULL,则将占位符绑定到PHP值null

You have to understand the difference between strings and PHP code. 您必须了解字符串和PHP代码之间的区别。

"null, PDO::PARAM_INT" IS a string. "null, PDO::PARAM_INT"是一个字符串。 You cannot store PHP code in a string. 您不能将PHP代码存储在字符串中。

"null" AGAIN is not a NULL value either but a string contains word " null "null" AGAIN也不是NULL值,但是字符串包含单词“ null

If you want ot bind a null, then just bind PHP NULL value . 如果要ot绑定null,则只需绑定PHP NULL value Yes, as simple as that. 是的,就这么简单。

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

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