Fixing dynamic unknown named parameters - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, April 9, 2023

Fixing dynamic unknown named parameters

In this snippet below which is used only as a SELECT statement for multiple rows of data, it gives a error on $stmt->bind_param($types, ...$values);:

mysqli_stmt::bind_param() does not accept unknown named parameters

I understand the error in general, but $Query is dynamic with the necessary question marks already in it but may have any number of $types and $values or maybe only one.

$results_array = [];
if (!empty($values)) :
    $stmt = $mysqli->prepare($Query);
    $stmt->bind_param($types, ...$values);
    $stmt->execute();
    $result = $stmt->get_result();
    while($row = $result->fetch_array()) :
        $results_array[] = $row;
    endwhile;
    $stmt->close();
    return $results_array;
endif;

$mysqli of course contains the connection string while $Query is the dynamic query being used which already contains the question marks and $types contains the related field types, ie "sssi" or whatever and matches the data in the $values array.

I started the conversion to prepared statements to my sites in 7.4 so any advice on code for 8.1, if different, would be great.

It seems to be working now thanks to the help below. Since some of this came from the answers and comments, I can't take credit for, I'll append it here to the question as the current problem appears to have been solved! I also elaborated on it a bit. This is a small part of a much larger function.

// For queries with parameters, use prepared statement
$results_array = [];
if (!empty($values) && str_contains($Query, '?')) :
    $stmt = $mysqli->prepare($Query);
    $stmt->bind_param($types, ...$values);
    $stmt->execute(array_values($values));
    $result = $stmt->get_result();
    if ($selType === "assoc") :
        while($row = $result->fetch_assoc()) :
            $results_array[] = $row;
        endwhile;
    else :                  
        while($row = $result->fetch_array()) :
            $results_array[] = $row;
        endwhile;
    endif;
    $stmt->close();
    return $stmt->get_result()->fetch_all();
// For queries without parameters, use mysqli 
else :
    if ($result = $mysqli->query($Query)) :     
        $numrowsCat = $result->num_rows;
        if ($numrowsCat >= 1) :
            if ($selType === "assoc") :
                while($row = $result->fetch_assoc()) :
                    $results_array[] = $row;
                endwhile;
            else :
                while($row = $result->fetch_array()) :
                    $results_array[] = $row;
                endwhile;
            endif;
            return $results_array;
        endif;
    endif;
endif;


source https://stackoverflow.com/questions/75962164/fixing-dynamic-unknown-named-parameters

No comments:

Post a Comment