【问题标题】:Error at fetch_row()[0], works fine on local serverfetch_row()[0] 出错,在本地服务器上工作正常
【发布时间】:2014-11-04 14:46:52
【问题描述】:

我有一个用 php 实现的分页功能(带有帮助教程)。它在本地服务器上运行良好。但是当上传到网络服务器时,它会显示语法错误。

错误:解析错误:语法错误,第 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 网络服务器:5.2

提前致谢,

【问题讨论】:

  • 如果代码中的用户名/密码组合是您的实际用户名/密码组合,您可能需要考虑在代码和/或服务器中更改它们以确保安全。

标签: php mysqli


【解决方案1】:

是的,这与您运行的 PHP 版本有关。数组解引用语法仅从 PHP 5.4 开始有效。所以,如果你的本地机器安装了 5.4,你可以点这个:

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

在以前的 PHP 版本中,您可以使用

获得相同的结果
list($total) = $dbc->query(...)->fetch_row();

【讨论】:

  • 感谢您的快速回答。得到它的工作。我只能在 3 分钟内接受答案。
猜你喜欢
  • 2011-11-11
  • 2013-09-20
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2021-09-08
  • 2013-04-18
相关资源
最近更新 更多