return 超过 1 mysql 个条目 bind_result

return more than 1 mysql entries with bind_result

我正在为我的商店创建一个页面,但我使用的是 get_result,在完成所有操作后,我意识到 HostGator 不允许我拥有 mysqlnd 运行。所以我不得不将所有内容都转换为 bind_result 但我在这里陷入了死胡同,可能是因为我必须更改所有内容而感到沮丧......

无论如何,我的问题是我调用了一些查询来读取 tables 但整个代码在返回 1 个结果后停止。例如,在这段代码中,我调用了一个约会列表,尽管我在数据库中设置了 5 个约会,但它只有 returns 第一个。它按预期工作并从多个 table 读取但在 1 个循环处停止。如果我不做 $listappointments_stmt->free_result(); 它不会让我继续阅读第二个 table; 首先在下一个查询之前,所以我认为这是我的问题,但我不知道如何解决它,因为如果我没有它,它会给我一个布尔错误。欢迎任何想法!

提前谢谢你, Xenos K.

<?php
                $query="SELECT * FROM appointments";
                $listappointments_stmt=$mysqli->prepare($query);
                $listappointments_stmt->execute();
                $listappointments_stmt->bind_result($AppId, $AppDate, $AppTime, $AppType, $AppArtist, $AppTitle, $AppClient, $AppPrice, $AppNotes);
                while ($listappointments_stmt->fetch())
                {
                $theDate=date("d-M-Y", strtotime($AppDate));
                echo "<tr><td>".$theDate."</td>";
                echo "<td>".$AppTime."</td>";
                $listappointments_stmt->free_result();
                $tempappointmentType=$AppType;
                $query2="SELECT * FROM appointmenttypes WHERE ID=?";
                $listappointmentsTypes_stmt=$mysqli->prepare($query2);
                $listappointmentsTypes_stmt->bind_param("s", $tempappointmentType);
                $listappointmentsTypes_stmt->execute();
                $listappointmentsTypes_stmt->bind_result($AppTypeId, $AppTypeName, $AppTypeColor);
                while ($listappointmentsTypes_stmt->fetch())
                {
                echo "<td><span class=\"label\" style=\"background-color:".$AppTypeColor."\">".$AppTypeName."</span></td>";
                }
                $listappointmentsTypes_stmt->free_result();
                $listappointmentsTypes_stmt->close();
                $tempappointmentArtist=$AppArtist;
                $query3="SELECT * FROM staff WHERE ID=?";
                $listappointmentsArtist_stmt=$mysqli->prepare($query3);
                $listappointmentsArtist_stmt->bind_param("s", $tempappointmentArtist);
                $listappointmentsArtist_stmt->execute();
                $listappointmentsArtist_stmt->bind_result($ArtId, $ArtName, $ArtNickName, $ArtSurname, $ArtPhone, $ArtBirthDate, $ArtIdentificationNumber, $ArtStreetName, $ArtStreetNumber, $ArtPostalCode, $ArtCity, $ArtCountry, $ArtPosition, $ArtEmail, $ArtFacebook, $ArtInstagram);
                while ($listappointmentsArtist_stmt->fetch())
                {
                echo "<td>".$ArtName." ".$ArtNickName." ".$ArtSurname."</td>";
                }
                $listappointmentsArtist_stmt->free_result();
                $listappointmentsArtist_stmt->close();
                echo "<td>".$AppTitle."</td>";
                $tempappointmentClient=$AppClient;
                $query4="SELECT * FROM clients WHERE ID=?";
                $listappointmentsClient_stmt=$mysqli->prepare($query4);
                $listappointmentsClient_stmt->bind_param("s", $tempappointmentClient);
                $listappointmentsClient_stmt->execute();
                $listappointmentsClient_stmt->bind_result($CliId, $CliName, $CliSurname, $CliPhone, $CliBirthDate, $CliIdentificationNumber, $CliStreetName, $CliStreetNumber, $CliPostalCode, $CliCity, $CliCountry, $CliFathersFullName, $CliMothersFullName, $CliEmail, $CliFacebook, $CliInstagram, $CliNotes);
                while ($listappointmentsClient_stmt->fetch())
                {
                echo "<td>".$CliName." ".$CliSurname."</td>";
                echo "<td>".$CliPhone."</td>";
                }
                $listappointmentsClient_stmt->free_result();
                $listappointmentsClient_stmt->close();
                echo "<td>".$AppPrice."</td>";
                echo "<td><a href=\"appointmentsedit.php?appointmentStatus=view&appointmentId=".$AppId."\" title=\"".$lang['view']."\"><i class=\"text-green fa fa-eye\"></i></a></td>";
                echo "<td><a href=\"appointmentsedit.php?appointmentStatus=edit&appointmentId=".$AppId."\" title=\"".$lang['edit']."\"><i class=\"text-blue fa fa-edit\"></i></a></td>";
                echo "<td><a href=\"appointmentsedit.php?appointmentStatus=delete&appointmentId=".$AppId."\" title=\"".$lang['delete']."\"><i class=\"text-red fa fa-trash-o\"></i></a></td></tr>";
                }
                $listappointments_stmt->close();
                ?>

学习在 SQL 中使用 JOIN 语句是明智的。这将允许您仅使用一个 SQL 查询来获取您需要的所有结果。

与此同时,如果您将一些 SQL 查询嵌套在其他查询中,则外部查询(在您的情况下 SELECT * FROM appointments)需要一个单独的数据库连接(在您的情况下 $mysqli ) 来自其余的查询。从连接发出新查询会重置其余查询。

专业提示:避免在生产软件中使用SELECT *。而是提供您需要的列列表。如果您这样做,您的软件将更加健壮且更易于理解。