PHP和库MySQLi - 绑定参数使用数组循环和存储? [英] PHP and MYSQLi - Bind parameters using loop and store in array?
问题描述
这将是更容易在未来code解释(这是错误的,顺便说一句):
It will be easier to explain with the next code (It's wrong, by the way):
$selectGenre_sql = 'SELECT genreID FROM genres WHERE dbGenre = ?';
if ($stmt->prepare($selectGenre_sql)) {
// bind the query parameters
$stmt->bind_param('s', $genre);
// bind the results to variables
$stmt->bind_result($genres);
// execute the query
$stmt->execute();
$genre = array();
while ($stmt->fetch()) {
$genre[] = $genres;
}
}
上面的code会从'genreID'的值时,dbGenre'等于'$流派。然后存储在数组中的结果。但i'ts不工作。为什么?我想是因为'$流派是一个数组,所以我需要循环槽的方式来获取genreID每一次不同的值。
The code above gets the value from 'genreID' when 'dbGenre' is equal to '$genre'. And then store the results in an array. But i'ts not working. Why? I think because '$genre' is an array, so I need to loop trough it to get a different value from 'genreID' each time.
$曲风包含电影类型枚举数组,例如:
$genre is an enumerated array containing movie genres, for example:
[0] =>动作[1] =>冒险[2] =>幻想
我需要比较DE值(如'行动'为例)
I need to compare de value (like 'Action' for example)
在'风格'表包含两列: genreID(INT)和 dbGenre(VARCHAR)
The 'genres' table contains two columns: genreID (INT) and dbGenre (VARCHAR)
的我只需要每个genreID(即数字)......可以说..当dbGenre等于操作,然后保存genreID在数组1,然后循环$类型数组来获得genreID下一个值和ARRAY1再存放起来的
我怎样才能解决这个问题?我是新来编程,所以请尽可能详细,你可以。谢谢!
How can I fix it?? I'm new to programming so please be as detailed as you can. Thanks!!
推荐答案
您不能绑定数组的SQL参数。您可以代替的单一文字值的的使用SQL参数。不是值列表,或前pression,或列名或表名。
You can't bind an array to an SQL parameter. You can use a parameter in SQL in place of a single literal value. Not a list of values, or an expression, or a column name or table name.
要解决的任务,你的情况,你可以使用两种解决方案:
To solve the task in your case, you can use either of two solutions:
解决方案一:遍历 $体裁
数组,每个值一次绑定,并执行的SQL查询每个值
First solution: loop over $genre
array, bind each value one at a time, and execute the SQL query for each value.
if ($stmt->prepare($selectGenre_sql)) {
$genre = array();
foreach ($gengre as $genreID) {
$stmt->bind_param('s', $genreID);
$stmt->execute();
$stmt->bind_result($genres);
while ($stmt->fetch()) {
$genre[] = $genres;
}
}
}
第二溶液:执行查询一次,用多个参数,一个用于所述阵列中的每个值。这需要一些棘手的code建立一个可变数量?
在SQL查询中的占位符,用逗号分隔的。
Second solution: execute the query once, with multiple parameters, one for each value in the array. This requires some tricky code to build a variable number of ?
placeholders in the SQL query, separated by commas.
$selectGenre_sql = 'SELECT genreID FROM genres WHERE dbGenre IN ('
. join(',', array_fill(0, count($genre), '?')) . ')';
if ($stmt->prepare($selectGenre_sql)) {
$genre = array();
. . .
此外,你需要变得棘手调用 bind_param()
的具有基于在 $体裁$ c中的元素参数个数可变$ C>数组:
. . .
call_user_func_array( array($stmt, 'bind_param'),
array_unshift($genre, str_repeat('i', count($genre)));
$stmt->execute();
$stmt->bind_result($genres);
while ($stmt->fetch()) {
$genre[] = $genres;
}
}
您可能要考虑使用 PDO :: mysql的
,因为它更容易从数组绑定参数。和mysqli接口是pretty尴尬这种情况。
You might want to consider using PDO::mysql
because it's easier to bind parameters from an array. The MySQLi interface is pretty awkward for this case.
这篇关于PHP和库MySQLi - 绑定参数使用数组循环和存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!