使用bind_result返回多个mysql条目

时间:2016-07-31 11:45:39

标签: php mysql mysqli mysqlnd

我正在为我的商店创建一个页面,但我使用的是get_result,在我完成所有操作后,我意识到HostGator无法让我运行mysql。所以我不得不将所有内容转换为bind_result,但我可能会感到沮丧,因为我必须改变一切......

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

提前谢谢你, 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();
                ?>

1 个答案:

答案 0 :(得分:1)

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

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

专业提示:避免在生产软件中使用SELECT *。而是提供您需要的列列表。如果你这样做,你的软件会更强大,更容易理解。