PHP MySQL Prepared Statements, build the lists of the types for bind_param on fly

I am trying to find a simple way to construct the string to pass in the Prepared Statements in MySQLi especially the types ‘isiis’.

I already have an array that contains the arguments to pass to the query and by cycling through it I can build another array by checking if every single value in the array is a number, I use the is_numeric() function, or not.

The problem is that by doing this the method that writes to the database, I use a class, crashes.

It does not crash if I instead build the array of types using the str_repeat() function and assigning all variables the value ‘s’.

Below is some code for understanding.

This is my $args array

Array
(
[0] => 1433
[1] => 2
[2] => 71
[3] => 0
[4] => hello
[5] => pippo
[6] => 1
[7] => 1
[8] => 1
[9] => 9
)

I just need to differentiate the numeric variables from the remainder so I do
something as…

$fields = array();

foreach ($args as $s) {

    if(is_numeric($s)){
        $fields[] = 'i';
        #echo "is numeric";
    }
    else{
        #echo "is other";
        $fields[] = 's';
    }

}

that return something as

echo '<pre>'; print_r($fields); echo '</pre>';


Array
(
    [0] => i
    [1] => i
    [2] => i
    [3] => i
    [4] => s
    [5] => s
    [6] => i
    [7] => i
    [8] => i
    [9] => i
)

now I retrieve data (‘iissi..etc,etc’) in two ways

$types = implode("", $fields);

$types2 = str_repeat('s',sizeof($args));


echo printf("function implode return %s type %s and has len  %s.", $types ,gettype($types), strlen($types));
echo "<br>";
echo printf("function str_repeat return %s type %s has len  %s.", $types2 ,gettype($types2), strlen($types2));

The first method, using the implode() function doesn’t work and return:

function implode return iiiissiiii type string and has len 10.63

The second method using the str_repeat() function work and return:

function str_repeat return ssssssssss type string has len 10.62

By the way I noticed that the length of the two strings is not an integer,
it should be 10 and it is also slightly different.

This is my warning.

Warning: mysqli::prepare(): Couldn’t fetch mysqli in /var/www/html/virtualab/engine/dbms.php on line 90

Fatal error: Uncaught Error: Call to a member function bind_param() on bool

Do you have any suggestions, what am I missing?

Thanks for your attention and sorry for the horrible code writing…