Joe_Schmoe
Joe_Schmoe

Reputation: 1484

bind_param difficulties

I'm having problems the following code gives me no results. however if I uncomment out the indicated line, and comment out the bind_param line it works, but isn't that defeating the purpose of mysqli? my var_dump gives my string(1) "1"

function teams($mysqli, $league_id) {
    echo 'league id = ' . var_dump($league_id);
    $sql = "SELECT team_id, team_name FROM teams where league_id='?'";
//  $sql = "SELECT team_id, team_name FROM teams where league_id='".$league_id."'";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('i', $league_id);
    $stmt->execute();
    $stmt->bind_result($col1, $col2);  
    while($stmt->fetch()) {
        $results[] = array(  
            'team_id' => $col1,  
            'team_name' => $col2  
        );  
    }  
    $stmt->close();
    var_dump($results);
    return $results;
}

Upvotes: 2

Views: 3798

Answers (1)

ray
ray

Reputation: 457

The function bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )

accepts the following $types

Type specification chars

Character Description

i corresponding variable has type integer

d corresponding variable has type double

s corresponding variable has type string

b corresponding variable is a blob and will be sent in packets

You are specifying the $types as 'i' and giving the value as string in single quotes. Remove the quotes and try to convert $league_id to int value.

http://php.net/manual/en/mysqli-stmt.bind-param.php

happy coding !!

Upvotes: 2

Related Questions