php& mysql数据库拉错误

时间:2014-03-13 12:37:58

标签: php mysql html5

你好,我有一个网站,我在php编码。我有一个数据库附加到它。我试图让它从数据库中读取数据到页面。我将数据库拆分为几个表。我以为我已经正确完成了代码,但我继续获取以下错误代码:

  

警告:mysqli_query()要求参数1为mysqli,在第13行的C:\ xampp \ htdocs \ stadium \ alpha \ a.php中给出整数

     

警告:mysqli_fetch_all()要求参数1为mysqli_result,在第14行的C:\ xampp \ htdocs \ stadium \ alpha \ a.php中给出null

     

注意:尝试在第15行的C:\ xampp \ htdocs \ stadium \ alpha \ a.php中获取非对象的属性

     

警告:mysqli_fetch_all()要求参数1为mysqli_result,在第16行的C:\ xampp \ htdocs \ stadium \ alpha \ a.php中给出null

我一直试图对此研究一段时间,而我无法做出正面或反面。我的数据提取和显示代码如下:

$sql = "SELECT colConferenceName.tblConference, colSchoolName.tblSchool, colClass.tblSchool, colSurfaceName.tblSurface, colSurfaceCompany.tblSurface, colStadiumName.tblStadium, colAddress.tblStadium, colCity.tblStadium, colRegion.tblStadium, colCounty.tblStadium, colCapacity.tblStadium, colSurfaceYear.tblSurface FROM tblStadium, tblConference, tblSurface, tblSchool WHERE tblConference.colConferenceID = tblSchool.colConferenceID AND tblSurface.colSurfaceID = tblStadium.colSurfaceID AND tblStadium.colStadiumID = tblStadiumSchool.colStadiumID AND tblSchool.colSchoolID = tblStadiumSchool.colSchoolID AND colStadiumName LIKE 'A%' ORDER BY colSchoolName";
$schoolinfo = mysqli_query($conn,$sql);
mysqli_fetch_all($schoolinfo,MYSQLI_ASSOC);
while (!$schoolinfo->EOF){ //looping through the recordset (until End Of File) 
while ($row = mysqli_fetch_all($schoolinfo,MYSQLI_ASSOC)){
    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>';
        }
    }

你可以提供给我的任何帮助都会很棒。提前谢谢。

关于连接,它是在一个单独的连接文件中完成的。它的方式是$ conn = require('xxx_xxx.xxx');我原则上询问输出以及如何修复编码错误。我xxx输出连接,因为我不希望人们有权访问文件名。我也在做大约80到100个连接,因此使用一个文件进行连接比编辑每个php文件的连接部分更有意义。另外我发现它可以通过这种方式获得更多安全性。

3 个答案:

答案 0 :(得分:1)

我没有看到你连接到mysql的时间点。试试mysqli_connect

$conn = mysqli_connect('<your-host>','<your-username>','<your-password>','<your-databasename>').

答案 1 :(得分:1)

您需要先连接到mysql服务器:

$ con = mysqli_connect($ host,$ username,$ password);

然后您需要选择您将使用的数据库: $ db = mysqli_db_select($ database);

你还必须设置正确的参数:$ host,$ username,$ password和$ database,你的mysql服务器必须先配置才能接受连接

好吧,你为什么不试试php框架呢?

答案 2 :(得分:0)

你可以试试这个:

请记住在脚本开头设置数据库登录

<?php

/* connect*/
$mysqli = new mysqli("localhost", "my_user", "my_password", "db_name");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* your sql query */
$query = "SELECT colConferenceName.tblConference, colSchoolName.tblSchool, ".
         "colClass.tblSchool, colSurfaceName.tblSurface, colSurfaceCompany.tblSurface, ".
         "colStadiumName.tblStadium, colAddress.tblStadium, colCity.tblStadium, ".
         "colRegion.tblStadium, colCounty.tblStadium, colCapacity.tblStadium, ".
         "colSurfaceYear.tblSurface FROM tblStadium, tblConference, tblSurface, tblSchool ".
         "WHERE tblConference.colConferenceID = tblSchool.colConferenceID AND ".
         "tblSurface.colSurfaceID = tblStadium.colSurfaceID AND ".
         "tblStadium.colStadiumID = tblStadiumSchool.colStadiumID AND ".
         "tblSchool.colSchoolID = tblStadiumSchool.colSchoolID AND ".
         "colStadiumName LIKE 'A%' ORDER BY colSchoolName";

/* query your db */
if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->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();

?>