Question

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.

There are a number of threads here on StackOverFlow (11692773, 1391777) that cover this topic but none of them seems to work for me.

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). I've tried using both PDO::PARAM_NULL and 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.

Can someone show me what I’m doing wrong?

Thanks in advance for any assistance you can offer.

Was it helpful?

Solution

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. 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. That makes it a string literal containing the word 'null', not a true 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. 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.

$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. Passing column names in the VALUES clause is not illegal in SQL, but it makes no sense to do it.

$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. This continues to be one of the most common mistakes among PHP developers!


Here's how I would write this code.

As you connect to PDO, enable exceptions. See 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);

OTHER TIPS

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". If you want a NULL, bind the placeholder to the PHP value null.

You have to understand the difference between strings and PHP code.

"null, PDO::PARAM_INT" IS a string. You cannot store PHP code in a string.

"null" AGAIN is not a NULL value either but a string contains word " null

If you want ot bind a null, then just bind PHP NULL value. Yes, as simple as that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top