使用“mysql_fetch_row”从数据库中检索结果并使用PHP和mysqli插入到数组中?

时间:2021-11-09 06:39:11

I need to retrieve data from several rows and then insert the results into an enumerated array so then I can use a "for" loop to echo it...

我需要从几行中检索数据,然后将结果插入到枚举数组中,然后我可以使用“for”循环来回显它...

I have this (I already connected to the database):

我有这个(我已经连接到数据库):

$genres_sql = 'SELECT genreID FROM genres WHERE imdbID = ?';
if ($stmt->prepare($genres_sql)) {
    // bind the query parameters
    $stmt->bind_param('i', $movieid);
    // bind the results to variables
    $stmt->bind_result($genre);
    // execute the query
    $stmt->execute();
    $stmt->fetch();
}

Here I get the first result (row) into a variable. But I need to insert it into an enumerated array so the I can echo each result using this:

在这里,我将第一个结果(行)放入变量中。但我需要将它插入枚举数组中,以便我可以使用以下方法回显每个结果:

if (!empty($genre)) {
for ($i = 0; $i + 1 < count($genre); $i++)
{
    echo $genre[$i].', '; 
}
echo $genre[$i];
}

This will echo: $genre[0], $genre[1], $genre[2], and so on until the last one.

这将回显:$ genre [0],$ genre [1],$ genre [2]等等,直到最后一个。

I know that mysql_fetch_row can do the work, but I'm new to programming so I need a very detailed explanation.. Thank you!!

我知道mysql_fetch_row可以完成这项工作,但我是编程的新手,所以我需要一个非常详细的解释..谢谢!!

4 个解决方案

#1


1  

You can loop using the MySQLi_Statement::fetch method:

您可以使用MySQLi_Statement :: fetch方法循环:

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

Basically fetch provides an iterator that the while can use to iterate through each result. The variables in bind_result (in this case $genre) are reassigned with each iteration.

基本上fetch提供了一个迭代器,while可以用来迭代每个结果。 bind_result中的变量(在本例中为$ genre)将在每次迭代时重新分配。

#2


2  

I'm not 100% familiar with mysqli but I've played with a lot of pgsql commands doing this sort of thing and i think what your looking for is mysqli-result->fetch_assoc. This will produce an associative array that you can loop over pretty easily like so:

我不是100%熟悉mysqli,但我玩了很多pgsql命令做这种事情,我认为你所寻找的是mysqli-result-> fetch_assoc。这将生成一个关联数组,您可以很容易地循环,如下所示:

while ($row = $result->fetch_assoc()) {
    printf ($row["genre"]);
}

EDIT: Alnitak has linked to better explanation than i have in the comments for this answer.

编辑:Alnitak与我在这个答案的评论中有更好的解释。

#3


1  

This isn't really an answer to the question, but if you want to print out an array as a comma-separated list, it's better to use the implode command than a for loop:

这不是问题的真正答案,但是如果要将数组打印为以逗号分隔的列表,最好使用implode命令而不是for循环:

//Replaces the for loop in the question
echo implode(', ', $genre);

...except that there's no comma after the last element.

...除了在最后一个元素之后没有逗号。

#4


0  

Just to complete @Mykroft reply, if you really just want an enumerated array just use $result->fetch_array(MYSQLI_NUM), although and associative array is much simple to use in most situations.

只是为了完成@Mykroft的回复,如果你真的只想要一个枚举数组,只需使用$ result-> fetch_array(MYSQLI_NUM),尽管在大多数情况下关联数组很容易使用。

#1


1  

You can loop using the MySQLi_Statement::fetch method:

您可以使用MySQLi_Statement :: fetch方法循环:

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

Basically fetch provides an iterator that the while can use to iterate through each result. The variables in bind_result (in this case $genre) are reassigned with each iteration.

基本上fetch提供了一个迭代器,while可以用来迭代每个结果。 bind_result中的变量(在本例中为$ genre)将在每次迭代时重新分配。

#2


2  

I'm not 100% familiar with mysqli but I've played with a lot of pgsql commands doing this sort of thing and i think what your looking for is mysqli-result->fetch_assoc. This will produce an associative array that you can loop over pretty easily like so:

我不是100%熟悉mysqli,但我玩了很多pgsql命令做这种事情,我认为你所寻找的是mysqli-result-> fetch_assoc。这将生成一个关联数组,您可以很容易地循环,如下所示:

while ($row = $result->fetch_assoc()) {
    printf ($row["genre"]);
}

EDIT: Alnitak has linked to better explanation than i have in the comments for this answer.

编辑:Alnitak与我在这个答案的评论中有更好的解释。

#3


1  

This isn't really an answer to the question, but if you want to print out an array as a comma-separated list, it's better to use the implode command than a for loop:

这不是问题的真正答案,但是如果要将数组打印为以逗号分隔的列表,最好使用implode命令而不是for循环:

//Replaces the for loop in the question
echo implode(', ', $genre);

...except that there's no comma after the last element.

...除了在最后一个元素之后没有逗号。

#4


0  

Just to complete @Mykroft reply, if you really just want an enumerated array just use $result->fetch_array(MYSQLI_NUM), although and associative array is much simple to use in most situations.

只是为了完成@Mykroft的回复,如果你真的只想要一个枚举数组,只需使用$ result-> fetch_array(MYSQLI_NUM),尽管在大多数情况下关联数组很容易使用。