2014-03-13 59 views
0

你好,我有一個網站,我在PHP編碼。我有一個數據庫附加到它。我試圖從數據庫中讀取數據到頁面中。我把數據庫分成幾個表格。我以爲我有代碼得當,但我一直在得到以下的錯誤代碼:php和mysql數據庫拉錯誤

Warning: mysqli_query() expects parameter 1 to be mysqli, integer given in C:\xampp\htdocs\stadium\alpha\a.php on line 13

Warning: mysqli_fetch_all() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\stadium\alpha\a.php on line 14

Notice: Trying to get property of non-object in C:\xampp\htdocs\stadium\alpha\a.php on line 15

Warning: mysqli_fetch_all() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\stadium\alpha\a.php on line 16

我一直在努力,現在研究這個了一會兒,我不能讓這個正面或反面。我的代碼爲數據拉和顯示如下:

$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文件的連接部分更有意義。另外我發現它可以有更多的安全性。

+1

你在哪裏設置你的'$ conn' – CodeBird

+0

$ conn = mysqli_conect()..... –

+0

**** connect **** mysqli_connect double n – CodeBird

回答

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

?> 
+0

嗯,我試過了,但它給了我錯誤,注意:未定義變量:mysqli在C:\ xampp \ htdocs \ stadium \ alpha \ a.php上線29 致命錯誤:調用成員函數query()on在第29行的C:\ xampp \ htdocs \ stadium \ alpha \ a.php中有一個非對象。我應該提到,我有一個連接的單獨文件,因爲我在大約80到100個獨立的php中連接到數據庫頁面。 – mdhof

1

我沒有看到你連接到mysql的時間點。嘗試mysqli_connect

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

謝謝,但連接不是問題。我不想顯示所有背景數據,原因很明顯。 – mdhof

1

您需要連接到MySQL服務器第一的開始設置你的數據庫登錄:

$ con = mysqli_connect($ host,$ username,$ password);那麼你需要選擇你要使用的數據庫: $ db = mysqli_db_select($ database);

你也必須設置正確paramenters:$主機,$的用戶名,$密碼和$數據庫和MySQL服務器都必須首先配置爲接受連接

很好,你爲什麼不嘗試一下PHP框架?