2015-06-21 33 views
1

從PHP/SQL開始,我在黑暗中磕磕絆絆,真的不知道如何進行bug測試,所以請原諒我對問題確切性質的模糊性。繼續。SQL選擇不輸出數據

我有一些代碼從SQL表中獲取類別ID,名稱和描述,並將結果保存到變量中。這是通過準備避免SQL注入的可能性來完成的。然後將該值輸入到一些PHP中,該值檢查查詢是否有任何響應,如果是,則將其打印到表中。

<?php 

//create_cat.php 
include_once (__DIR__ . '/../includes/db_connect.php'); 
include_once (__DIR__ . '/../includes/functions.php'); 

include_once (__DIR__ . '/header.php'); 
ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); 
$stmt = "SELECT 
      cat_id, 
      cat_name, 
      cat_description 
     FROM 
      categories"; 
if (login_check($mysqli) == true) { 
    if(!$result = $mysqli->query($stmt)){ 
     echo 'The categories could not be displayed, please try again later.'; 
    } else { 
     if ($result->num_rows === 0) { 
      echo 'No categories defined yet.'; 
     } else { 
      //prepare the table 
      echo '<table border="1"> 
       <tr> 
       <th>Category</th> 
       <th>Last topic</th> 
       </tr>'; 

      while ($row = $result->fetch_assoc()) { 
       echo '<tr>'; 
       echo '<td class="leftpart">'; 
       echo '<h3><a href="category.php?id">' . $row['cat_name'] . '</a></h3>' . $row['cat_description']; 
       echo '</td>'; 
       echo '<td class="rightpart">'; 
       echo '<a href="topic.php?id=">Topic subject</a> at 10-10'; 
       echo '</td>'; 
       echo '</tr>'; 
      } 
     } 
    } 
} else { 
    echo <<<error 
    <p> 
    <span class="error">You are not authorized to access this page.</span> Please <a href="../index.php">login</a>. 
    </p> 
error; 
} 
include_once (__DIR__ . '/footer.php'); 
?> 

但是,表SQL表格肯定有值,但PHP只輸出:「類別無法顯示,請稍後再試。」

+0

以下行添加到你的PHP代碼的頂部,他們應該幫你調試的問題:'的ini_set(「display_errors設置」,1); ini_set('display_startup_errors',1); error_reporting(-1);' –

+0

另請參閱'store_result()'[here](https://php.net/manual/en/mysqli.store-result.php)的文檔,因爲您應該存儲該值變成一個變量,然後分析變量。你的發言似乎有些混亂。請參閱[這裏](http://codular.com/php-mysqli)關於使用mysqli的教程。 –

+0

你的代碼混合了'mysqli_'和'mysql_'的功能。不要這樣做。僅使用'mysqli_'函數。 – spencer7593

回答

1

好的,讓它工作。我刪除了$ stmt_prep命令,確保只使用mysqli命令,並修復了一些語法錯誤。代碼仍然破壞HTML,但我所問的問題已修復。

0

如果你想保持面向對象的風格,主要的錯誤是在獲取結果之前關閉語句。 PHP將在腳本末尾釋放語句,但是一旦您調用$stmt->close();,您將無法從查詢中讀取任何數據。由於PHP通過引用複製,$result = $stmt;不復制數據,它只是引用相同的已關閉語句。

獲取語法也不完全符合您的要求:您需要將一些佔位符變量與例如$stmt->bind_result($name, $description);綁定,然後調用$stmt->fetch()而不是fetch_assoc。

我並不清楚login_check確實在做什麼,但在我看來,您希望提前進行此檢查,以便在用戶未經授權的情況下不執行查詢。

你原來的代碼最終會看起來像:

<?php 

if (login_check($mysqli) == true) { 
    $stmt = $mysqli->prepare($prep_stmt); 
    if ($stmt) { 
     $stmt->execute(); 
     $stmt->store_result(); 
     if ($stmt->num_rows == 0) { 
      echo 'No categories defined yet.'; 
     } else { 
      //prepare the table 
      echo '<table border="1"> 
       <tr> 
       <th>Category</th> 
       <th>Last topic</th> 
       </tr>'; 

      $stmt->bind_result($name, $description); 
      while ($stmt->fetch()) { 
       echo '<tr>'; 
       echo '<td class="leftpart">'; 
       echo '<h3><a href="category.php?id">' . $name . '</a></h3>' 
        . $description; 
       echo '</td>'; 
       echo '<td class="rightpart">'; 
       echo '<a href="topic.php?id=">Topic subject</a> at 10-10'; 
       echo '</td>'; 
       echo '</tr>'; 
      } 
     } 
    } else { 
     echo 'The categories could not be displayed, please try again later.'; 
    } 
} else { 
    echo <<<error 

    <p> 
     <span class="error">You are not authorized to access this page.</span> Please 
     <a href="../index.php">login</a>. 

    </p> 
error; 
}