Search code examples
phpmysqli

PHP: Fatal error: Call to undefined method mysqli::SELECT


Okay I asked a question last week. I got a few answers, several which did not help; however, I decided to recheck all of my connections. That did resolve some problems after I had to check here to make it function. Next was for some reason I did not check my query to make sure it worked in mysql. I also noticed that my version has a way to output the query into PHP code which I did. Now I am stuck with the error code:

Fatal error: Call to undefined method mysqli::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() in C:\xampp\htdocs\stadium\alpha\a.php on line 24

Now here is my new code, I will try to make it look halfway decently,

    $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 = "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";

/* 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();

Now I am not sure what the issue is. I been trying to get this to work. BTW just so you know the query and connection should not be an issue. If their is something I am missing just let me know.


Solution

  • There is your problem:

    if ($result = $mysqli->$sql($sql)) {
    

    You're basically calling a method with the same name as your SQL statement, i.e. SELECT tblConference [...]. Such a method doesn't exist, obviously. Rather, try mysqli::query:

    if ($result = $mysqli->query($sql)) {
    

    By the way, another error is in this line:

    while ($row = $sql->fetch_assoc())
    

    $sql is still your SQL statement. What you want is to iterate over the mysqli_result object, which you've put into the variable $result:

    while ($row = $result->fetch_assoc())