Hi,

I would like to ask why I am getting the above error for this code:

if (isset($_GET['msg'])) { $mailbox_message = $_GET['msg']; } else { $mailbox_message = ""; }

then:

if (!strlen($mailbox_message)) { $mysql_query->bindParam(':param_message_mailbox', $mailbox_message = NULL, PDO::PARAM_NULL); }
else { $mysql_query->bindParam(':param_message_mailbox', $mailbox_message, PDO::PARAM_STR); }

    Because, passing by reference means you are passing a reference to the memory location used by whatever variable you supply. You are not supplying a variable, you are supplying a value (NULL)

    The value of these expression are null and 5, not $var and $var2

    $var = null;
    $var2 = 5;
    

    And passing values by reference makes no sense. You can only pass variables by reference.

    $stmt = $db->prepare('SELECT * FROM table WHERE field = :message');
    
    /* Binding as a parameter means binding to the variable's reference, i.e. its memory location.
     * Therefor you must have defined the variable first
     */
    $message;
    $stmt->bindParam(':message', $message, PDO::PARAM_STR);
    
    $message = 'I will be selected';
    $stmt->execute();
    
    $message = 'Now it is my turn';
    $mysql_query->execute();
    
    function callingCodeVariableUpdated(&$out) { $out = 'Changed again'; }
    callingCodeVariableUpdated($message);
    $mysql_query->execute();
    

    While this would make no sense at all

    callingCodeVariableUpdated(5);
    

    which would imply

    5 = 'Changed again';
    

    Also you do not have to bind an empty string as null. While there is a difference between the empty string and null

    INSERT INTO a(id, txt)
    VALUES
    (1, ''),
    (2, null);
    
    -- finds: 1, ''
    SELECT *
    FROM a
    WHERE txt = '';
    
    -- finds: 2, null
    SELECT *
    FROM a
    WHERE txt IS NULL;
    

    Is there really a point to not simply bind to $message_mailbox as PARAM_STR?

      $message_mailbox is just a $_GET value so if the url has the string msg the n I want to assign in $message_mailbox but if not then I want it to be null..

        jrahma;11034759 wrote:

        then I want it to be null..

        Let me rephrase that question then…

        Why do you NOT want it to be an empty string?

          no reason... but I though making it null would be best especially when passing to the MySQL and dealing with it as NULL

            It will give you no additional benefit, and you have created a special case (null) which requires its own handling, instead of having one case (string). And you lose no information by handling it as a string either. You can still distinguish '' from 'any other string'.

              Write a Reply...