MYSQLI prepared statement bind_param types does not work

后端 未结 2 519
余生分开走
余生分开走 2021-01-26 00:45

I have been using prepared insert statements for some years and assumed it was binding parameters properly or would give an error but it seems not as the following php binds and

相关标签:
2条回答
  • 2021-01-26 00:55

    $stmt->bind_param() doesn't check the given variables for a certain type, it only converts them into the specified type. And your string 'aaaaaaa' is converted into an int-value: 0. That's the way php does it.

    The database insert statement is the wrong place to check, if your variables contain useful/correct values. Do that before and only try to insert them, if your validations work.

    To do the validation for an int, you could use the php-function is_numeric() or is_int().

    0 讨论(0)
  • 2021-01-26 00:59

    I'm not an expert for sure, but at first look. You have:

    $id1 = 'aaaaaaa';
    $id2= 'aaaaaaa';
    $result = $stmt->bind_param('ii', $id1, $id2);
    

    Thing is your 'ii' parameter says that you will be binding integers! And in fact your $id1 and $id2 are strings. For strings you should go with:

    $result = $stmt->bind_param('ss', $id1, $id2);
    
    0 讨论(0)
提交回复
热议问题