bind_param 困难 [英] bind_param difficulties
问题描述
我遇到了问题,以下代码没有给我任何结果.但是,如果我取消注释指示的行,并注释掉 bind_param 行,它会起作用,但这不是违背了 mysqli 的目的吗?我的 var_dump 给了我的 string(1) "1"
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;
}
推荐答案
函数 bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )
The function bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] )
接受以下$types
类型规范字符
人物描述
i 对应变量的类型为整数
d 对应变量的类型为 double
d corresponding variable has type double
对应的变量是字符串类型
s corresponding variable has type string
b 对应的变量是一个 blob,会分包发送
b corresponding variable is a blob and will be sent in packets
您将 $types 指定为 'i' 并在单引号中以字符串形式给出值.删除引号并尝试将 $league_id 转换为 int 值.
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
快乐编码!!
这篇关于bind_param 困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!