2014-11-04 151 views
0

我有一個在php中實現的分頁函數(附帶幫助教程)。它在本地服務器上正常工作。但是當上傳到Web服務器時,它顯示語法錯誤。fetch_row()錯誤[0],在本地服務器上正常工作

錯誤:解析錯誤:語法錯誤,意想不到的 '[' 上線46

46號線:

')->fetch_row()[0]; 

這裏是我的全碼:

 <?php 
     if(isset($_POST['delete'])) { 
      $id = $_POST['delete']; 
      // Connect to the database 
    $dbc = mysqli_connect('xxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxxxx', 
    'xxxxxxxxxxxxxxxx') 
       or die('Error connecting to MySQL server.'); 

      $query = "DELETE FROM questions WHERE question_id = '$id'"; 

      $result = mysqli_query($dbc, $query) 
       or die('Error querying database.'); 
      mysqli_close($dbc); 
     } 
     try { 
$dbc = mysqli_connect('xxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxx', 'xxxxxxxxxxxxxxx', 
    'xxxxxxxxxxxxxxxx') 
      or die('Error connecting to MySQL server.'); 

      // Find out how many items are in the table 
      $total = $dbc->query(' 
      SELECT 
       COUNT(*) 
      FROM 
       questions 
      ')->fetch_row()[0]; 

      // How many items to list per page 
      $limit = 3; 

      // How many pages will there be 
      $pages = ceil($total/$limit); 

      // What page are we currently on? 
      $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
      'options' => array(
      'default' => 1, 
      'min_range' => 1, 
      ), 
      ))); 

      // Calculate the offset for the query 
      $offset = ($page - 1) * $limit; 

      // Some information to display to the user 
      $start = $offset + 1; 
      $end = min(($offset + $limit), $total); 

      // The "back" link 
      $prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>'; 

      // The "forward" link 
      $nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>'; 

      // Display the paging information 
      echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>'; 

      // Retrieve the score data from MySQL 
      $query = "SELECT * FROM questions ORDER BY question_id LIMIT $offset, $limit"; 
      $data = mysqli_query($dbc, $query); 
      echo "<div class=\"questions_table\">"; 
      if (mysqli_num_rows($data)>0) { 
       echo "<form method=\"POST\">"; 
       echo "<table>"; 
       echo "<tr><th>Question Type</th><th>Question Text</th><th>option 1</th><th>option 2</th>" 
        ."<th>option 3</th><th>option 4</th><th>Answer</th><th>Delete Question</th></tr>"; 
       // Display the results 
       $i = 0; 
       while ($row = mysqli_fetch_array($data)) { 
        echo "<tr>"; 
        echo '<td>'. $row['question_type']. '</td>'; 
        echo '<td>'. $row['question_text']. '</td>'; 
        echo '<td>'. $row['option1']. '</td>'; 
        echo '<td>'. $row['option2']. '</td>'; 
        echo '<td>'. $row['option3']. '</td>'; 
        echo '<td>'. $row['option4']. '</td>'; 
        echo '<td>'. $row['answer']. '</td>'; 
        echo "<td><button class='ph-button ph-btn-red ph-button-delete' type='submit' value='".$row['question_id']."' name='delete'>Delete</button></td><br>"; 
        echo "</tr>"; 
       } 
       echo "</table>"; 
       echo "</form>"; 
       echo "</div>"; 

      } else { 
       echo '<p>There are no questions in the database. Please add them.</p>'; 
      } 

     } catch (Exception $e) { 
      echo '<p>', $e->getMessage(), '</p>'; 
     } 
    ?> 

是否相關到PHP版本?誰能幫我這個?

PHP版本: 在我的本地服務器:5.5.15 Web服務器:5.2

在此先感謝,

+0

如果在代碼中的用戶名/密碼組合相同的結果你的實際的,你可能想考慮改變他們在代碼和/或服務器的安全性。 – gabe3886 2014-11-04 14:50:45

+0

感謝提醒... – 2014-11-04 14:53:00

回答

0

是的,這關係到你正在運行的PHP版本。數組解引用語法僅在PHP 5.4中有效。所以,如果你的本地計算機上安裝了5.4,你可以點這一點:

$total = $dbc->query(...)->fetch_row()[0]; 

在PHP以前的版本,你可以實現與

list($total) = $dbc->query(...)->fetch_row(); 
+0

感謝您的快速回答。得到它的工作。我只能接受3分鐘的答案。 – 2014-11-04 14:58:19

相關問題