Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - How to bind mysqli parameters using loop and store results in array?

$genre = array(
    'Action',
    'Adventure',
    'Fantasy'
);
$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();
    $array1 = array();
    while ($stmt->fetch()) {
        $array1[] = $genres;
    }
}

The code above gets the value from genreID when dbGenre is equal to $genre. And then store the results in an array. But it's not working because $genre is an array, so I need to loop through it to get a different value from genreID each time.

The 'genres' table contains two columns: genreID (INT) and dbGenre (VARCHAR)

I just need each genreID (that is a number)... Let's say when dbGenre is equal to Action, then store the genreID in an array1, and then loop the $genre array to get the genreID for the next value and store it again in array1.

How can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

First solution: loop over $genre array, bind each value one at a time and execute the SQL query for each value.

$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;
    }
}

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), '?')) . ')';

Also you need to get tricky calling bind_param() with a variable number of arguments based on the elements in your $genre array:

$stmt->prepare($selectGenre_sql);
$temp = array();
foreach ($genre as $key => $value) {
    $temp[] = &$genre[$key];
}

array_unshift($genre, str_repeat('i', count($genre)));
call_user_func_array(array($stmt, 'bind_param'), $genre);

$stmt->execute();

$stmt->bind_result($genres);

$array1 = array();
while ($stmt->fetch()) {
    $array1[] = $genres;
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...