Why does bind_param work when my prepared statement doesn't have any placeholders?

51 Views Asked by At

I ran across something I'm not sure on why it is working. Below I use bind_param and I forgot to set the proper parameter types but somehow it works. By working I mean that the results I get are that the data retrieved is correct. Look at returned values below.

Why is this working and is there any problem with doing it this way ?

Should I remove the bind_param?

I want to keep this as a prepared statement. Would removing the bind_param cause that to stop being a prepared statement?

function getDeviceTypes($conn)
{
    $stmt = $conn->prepare("SELECT deviceTypes_id, deviceTypes_name FROM deviceTypes");
    if (!$stmt)
    {
        header("location: ../index.php?error=cannot_get_device_types");
        exit();
    }
    $types = "";
    $stmt->bind_param("s", $types); // Question about this.
    $stmt->execute();

    $result = $stmt->get_result(); // get the mysqli result
    $data = $result->fetch_all(MYSQLI_ASSOC);
    
    $stmt->close();
    return $data;
}

Returned values

foreach ($data as $d)
{
    // The value is set to the correct id and the name is correct. But I only bind one param ?
    echo "<option value='" . $d['deviceTypes_id'] . "'>" . $d['deviceTypes_name'] . "</option>";
}
0

There are 0 best solutions below