Hey there,

I have posted a little piece of code at http://pastebin.org/9683 - it's a prepared query (mysqli).

It works fine as long as I'm inserting some actual values. My problem is that in cases where $name is empty, I want to insert the MySQL null value instead of an actual value.

This can't be done just by $stmt->bind_param('is', $user_id, 'NULL'), as this will insert "null" as a string.

I found one way to solve the problem - by writing $sql = '... VALUES (?, NULL)' instead of $sql = '... VALUES (?, ?)' - But this is a very bad and inflexible solution.

I know that I can just use regular queries, but if possible, I would really like to stick to prepared queries!

Any suggestion to how to solve this problem smoothly?

Best regards

Julius

    I believe it should be:

    $stmt->bindParam('is', $user_id, PDO::PARAM_NULL);

    EDIT:
    Whoops, mysqli, right. My solution is for the PDO extension. Looking at the PHP manual for the MySQLi extension, I must say I cannot find an equivalent. Sorry.

    Ah, I searched and found this suggestion:

    Doesn't bind_param support the PHP null value?

    $var1= null;
    $var2= null;
    $var3= null;

    You could give it a try and see if it works.

      It works perfectly! Doh, so simple :-)

      Thanks a lot!

        You're welcome 🙂
        Remember to mark this thread as resolved (if it is) using the thread tools.

          Write a Reply...