Domanda

In una dichiarazione mysqli preparata, un NULL viene trasformata in '' (nel caso di una stringa) o 0 (nel caso di un numero intero). Vorrei conservarlo come un vero NULL. C'è un modo di fare questo?

È stato utile?

Soluzione

So che questo è un vecchio thread, ma è possibile associare un vero valore NULL per le istruzioni preparate (leggi questo ).

  

È possibile, infatti, utilizzare mysqli_bind_parameter passare un valore NULL nel database. è sufficiente creare una variabile e memorizzare il valore NULL (vedi la pagina di manuale per esso) alla variabile e legare questo. Funziona alla grande per me comunque.

Così dovrà essere qualcosa del tipo:

<?php
    $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

    // person is some object you have defined earlier
    $name = $person->name();
    $age = $person->age();
    $nickname = ($person->nickname() != '') ? $person->nickname() : NULL;

    // prepare the statement
    $stmt = $mysqli->prepare("INSERT INTO Name, Age, Nickname VALUES (?, ?, ?)");

    $stmt->bind_param('sis', $name, $age, $nickname);
?>

Questo dovrebbe inserire un valore NULL nel database.

Altri suggerimenti

Per chi viene a guardare questo perché stanno avendo problemi NULL vincolante nella loro WHERE dichiarazione, la soluzione è questa:

C'è un mysql NULL operatore sicuro che deve essere utilizzato:

<=>

Esempio:

<?php
$price = NULL; // NOTE: no quotes - using php NULL
$stmt = $mysqli->prepare("SELECT id FROM product WHERE price <=> ?"); // Will select products where the price is null
$stmt->bind_param($price);
?>

The comments to the PHP documentation on mysqli_stmt::bind_param indicate that passing in NULL was not easily possible.


Please see @creatio's answer: https://stackoverflow.com/a/6892491/18771


Solutions offered in the comments do some pre-preparation work on the prepared statement, replacing the "?" markers with "NULL" for every param that has the PHP null value. The modified query string is then used.

The following function is from user comment 80119:

function preparse_prepared($sQuery, &$saParams)
{
    $nPos = 0;

    $sRetval = $sQuery;
    foreach ($saParams as $x_Key => $Param)
    {
        //if we find no more ?'s we're done then
        if (($nPos = strpos($sQuery, '?', $nPos + 1)) === false)
        {
            break;
        }

        //this test must be done second, because we need to 
        //increment offsets of $nPos for each ?.
        //we have no need to parse anything that isn't NULL.
        if (!is_null($Param))
        {
            continue;
        }


        //null value, replace this ? with NULL.
        $sRetval = substr_replace($sRetval, 'NULL', $nPos, 1);

        //unset this element now
        unset($saParams[$x_Key]);
    }
    return $sRetval;
} 

(It's not really the coding style I would have done it in, but if it works...)

by my side, i store every parameters in an array and pass them in Bind_param function by array_shift($myArray). NULL is accepted like that.. S.

<?php
$mysqli=new mysqli('localhost','root','','test');
$mysqli->query("CREATE TABLE test_NULL (id int(11))");
if($query=$mysqli->prepare("insert into test_NULL VALUES(?)")){
    $query->bind_param('i',$null); //note that $null is undefined
    $query->execute();
}else{
    echo __LINE__.' '.$mysqli->error;
}
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top