好吧,我上周问了一个问题。我得到了一些答案,其中一些没有帮助;然而,我决定重新检查我的所有连接。在我必须检查此处以使其正常运行之后,这确实解决了一些问题。接下来是由于某种原因我没有检查我的查询以确保它在 mysql 中工作。我还注意到我的版本有一种将查询输出到 PHP 代码中的方法,我就是这么做的。现在我被错误代码困住了:
致命错误:调用未定义的方法 mysqli::SELECT tblConference.colConferenceName、tblSchool.colSchoolName、tblSchool.colClass、tblSurface.colSurfaceName、tblSurface.colSurfaceCompany、tblStadium.colStadiumName、tblStadium.colAddress、tblStadium.colCity、tblStadium.colRegion ,tbl体育场。 colCounty、tblStadium.colCapacity 来自 tblStadium、tblConference、tblSurface、tblSchool、tblStadiumSchool,其中 tblConference.colConferenceID = tblSchool.colConferenceID AND tblSurface.colSurfaceID = tblStadium.colSurfaceID AND tblStadium.colStadiumID = tblStad iumSchool.colStadiumID 和 tblSchool.colSchoolID = tblStadiumSchool.colSchoolID 和 tblSchool。 colSchoolName LIKE \'A%\' ORDER BY tblSchool.colSchoolName() in C:\xampp\htdocs\stadium lpha .php 第 24 行
现在这是我的新代码,我会尽力让它看起来像样一点,
$mysqli = new mysqli("localhost", "root", "", "dbstadium");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error);
exit();
}
/* your sql query */
$sql = "选择 tblConference.colConferenceName、tblSchool.colSchoolName、tblSchool.colClass、tblSurface.colSurfaceName、tblSurface.colSurfaceCompany、tblStadium.colStadiumName、tblStadium.colAddress、tblStadium.colCity、tblStadium.colRegion、tblStadium.colCount y,tblStadium.colCapacity FROM tblStadium、tblConference、tblSurface、tblSchool、tblStadiumSchool 其中 tblConference.colConferenceID = tblSchool.colConferenceID 和 tblSurface.colSurfaceID = tblStadium.colSurfaceID 和 tblStadium.colStadiumID = tblStadiumSchool.colStadiumID 和 tblSchool.col SchoolID = tblStadiumSchool.colSchoolID 和 tblSchool.colSchoolName LIKE \'A% \' 按 tblSchool.colSchoolName 排序";
/* query your db */
if ($result = $mysqli->$sql($sql)) {
/* fetch associative array */
while ($row = $sql->fetch_assoc())
{ /* display row */
echo '<p>School Name: ' . $row['colSchoolName'] . '</br>Conference: ' . $row['colConferenceName'] . '</br>Class: ' . $row['colClass'] . '</br>Stadium Name: ' . $row['colStadiumName'] . '</br>Address: ' . $row['colAddress'] . '</br>City: ' . $row['colCity'] . '</br>County: ' . $row['colCounty'] . '</br>Region: ' . $row['colRegion'] . '</br>Capacity: ' . $row['colCapacity'] . '</br>Surface Type: ' . $row['colSurfaceName'] . '</br>Surface Company: ' . $row['colSurfaceCompany'] . '</br>Year Installed: ' . $row['colSurfaceYear'] . '</p>';
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
现在我不确定问题是什么。我一直在努力让它发挥作用。顺便说一句,只是让您知道查询和连接不应该成为问题。如果我缺少它们,请告诉我。
你的问题是:
if ($result = $mysqli->$sql($sql)) {
您基本上是在调用与 SQL 语句同名的方法,即
SELECT tblConference [...]
。显然,这样的方法不存在。相反,尝试mysqli::query
:
if ($result = $mysqli->query($sql)) {
顺便说一下,这一行还有另一个错误:
while ($row = $sql->fetch_assoc())
$sql
仍然是你的SQL语句。您想要的是迭代 mysqli_result
对象,并将其放入变量 $result
:
while ($row = $result->fetch_assoc())
这样应该可以了
$mysqli = new mysqli("localhost", "root", "", "dbstadium");
if (mysqli_connect_errno()) { // check connection
printf("Connect failed: %s\n", mysqli_connect_error);
exit();
}
// your sql query
$sql = "SELECT tblConference.colConferenceName, tblSchool.colSchoolName, tblSchool.colClass, tblSurface.colSurfaceName, tblSurface.colSurfaceCompany, tblStadium.colStadiumName, tblStadium.colAddress, tblStadium.colCity, tblStadium.colRegion, tblStadium.colCounty, tblStadium.colCapacity FROM tblStadium, tblConference, tblSurface, tblSchool, tblStadiumSchool WHERE tblConference.colConferenceID = tblSchool.colConferenceID AND tblSurface.colSurfaceID = tblStadium.colSurfaceID AND tblStadium.colStadiumID = tblStadiumSchool.colStadiumID AND tblSchool.colSchoolID = tblStadiumSchool.colSchoolID AND tblSchool.colSchoolName LIKE \'A%\' ORDER BY tblSchool.colSchoolName";
if ($result = $mysqli->query($sql)) { // query your db
while ($row = $result->fetch_object())// fetch object
{ // display row
echo "<p><div>School Name: {$row->colSchoolName}</div><div>Conference: {$row->colConferenceName}</div><div>Class: {$row->colClass}</div><div>Stadium Name: {$row->colStadiumName}</div><div>Address: {$row->colAddress}</div><div>City: {$row->colCity}</div><div>County: {$row->colCounty}</div><div>Region: {$row->colRegion}</div><div>Capacity: {$row->colCapacity}</div><div>Surface Type: {$row->colSurfaceName}</div><div>Surface Company: {$row->colSurfaceCompany}</div><div>Year Installed: {$row->colSurfaceYear}</div></p>";
}//end while loop
$result->free(); // free result set
}//end if
$mysqli->close(); // close connection