PHP5 sqli bind_param problem with binding boolean values

By : Steve
Source: Stackoverflow.com
Question!

I have a problem binding booleans using mysqli_stmt::bind_param in PHP5.

The SQL query is the following: insert into nvp_notes (subject,messageid,receivedate,read) values (?,?,?,?)

Where 'read' is a tinyint, either 0 or 1, as I've had issues with bit using mysqli. So the types that I list in bind_param are:

$stmt->bind_param('sdsd', ...);

I've also tried 'sdsb' and 'sdss' but nothing seems to work, and I always get the message:

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

When I remove the read field in the statement everything works fine. I've run out of ideas with this one. Surely bind_param works with booleans??

By : Steve


Answers

As what you need to bind is an Integer, I would swtich the 'd'(double) by 'i'

$stmt->bind_param('sisi', ...);

By : urnenfeld


Yet Another Swallow Solution:

sub pp {
  my $h = shift();
  qq[{${\(join',',map"$_=>$h->{$_}",keys%$h)}}]
}
print pp({a => 1, b => 2, c => 3});

But use Data::Dumper instead.

For very fancy output you can use also:

use Data::Dumper;
use Perl::Tidy;
sub pp {
        local $Data::Dumper::Terse    = 1;
        local $Data::Dumper::Indent   = 0;
        my $source = Dumper(@_);
        my $result;
        Perl::Tidy::perltidy(
                source      => \$source,
                destination => \$result,
                argv        => [qw(-pbp -nst)]
        );
        return $result;
}

If you prefer some keys should be first than you can use this approach (i want type first and position second):

    local $Data::Dumper::Sortkeys = sub {
            [   sort {
                            if    ( $b eq 'type' )     {1}
                            elsif ( $a eq 'type' )     {-1}
                            elsif ( $b eq 'position' ) {1}
                            elsif ( $a eq 'position' ) {-1}
                            else                       { $a cmp $b }
                            } keys %{ $_[0] }
            ];
    };


This video can help you solving your question :)
By: admin