意外的是(T_IF)解析器错误

时间:2014-05-29 23:35:20

标签: php

第15行意外的是(T_IF),我知道它可能是我没有关闭的东西,也许我刚刚盯着屏幕太久但我找不到它。

 <?php

$response=array();


if (!empty($_GET["page"])){
    $Page = $_GET['page'];
    $limit= (int)page*10;

      $db = mysqli_connect('127.0.0.1', 'root', '', 'CampusList') or die(mysqli_error());
// get all products from products table
$result = mysqli_query($db, "SELECT * FROM post order by post_date desc limit $limit") or die(mysqli_error());
 

if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["post"] = array();
 
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $post = array();
        $post["post_id"] = $row["post_id"];
        $post["post_name"] = $row["post_name"];
        $post["post_creator"] = $row["post_creator"];
        $post["post_type"] = $row["post_type"];
        $post["post_school_id"] = $row["post_school_id"];
        $post["post_content"] = $row["post_content"];
        $post["price"] = $row["price"];
        $post["post_date"] = $row["post_date"];
        $post["post_class_id"] = $row["post_class_id"];
        $post["post_flagged"] = $row["post_flagged"];
 
        // push single product into final response array
        array_push($response["post"], $post);
    }
    // success
    $response["success"] = 1;
 
    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No Posts found";
 
    // echo no users JSON
    echo json_encode($response);
}
}else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Error Please try again";

    // echoing JSON response
    echo json_encode($response);
}
?>

这只是一个php激活的脚本,可以从数据库中返回10个条目,没有什么花哨的,是的,由于某种原因,我没有捕捉到导致此错误的原因,谢谢你的帮助!

2 个答案:

答案 0 :(得分:3)

您错过了$前面的page,这应该是一个大写的P,因为您将$Page用于$Page = $_GET['page'];mysql_num_rows

PHP变量区分大小写。

您还混合了MySQL API,它们不能混合在一起。

mysqli_num_rows应为mysql_fetch_arraymysqli_fetch_array应为<?php $response=array(); if (!empty($_GET["page"])){ $Page = $_GET['page']; $limit= (int)$Page*10; $db = mysqli_connect('127.0.0.1', 'root', '', 'CampusList') or die(mysqli_error()); // get all products from products table $result = mysqli_query($db, "SELECT * FROM post order by post_date desc limit $limit") or die(mysqli_error()); if (mysqli_num_rows($result) > 0) { // looping through all results // products node $response["post"] = array(); while ($row = mysqli_fetch_array($result)) { // temp user array $post = array(); $post["post_id"] = $row["post_id"]; $post["post_name"] = $row["post_name"]; $post["post_creator"] = $row["post_creator"]; $post["post_type"] = $row["post_type"]; $post["post_school_id"] = $row["post_school_id"]; $post["post_content"] = $row["post_content"]; $post["price"] = $row["price"]; $post["post_date"] = $row["post_date"]; $post["post_class_id"] = $row["post_class_id"]; $post["post_flagged"] = $row["post_flagged"]; // push single product into final response array array_push($response["post"], $post); } // success $response["success"] = 1; // echoing JSON response echo json_encode($response); } else { // no products found $response["success"] = 0; $response["message"] = "No Posts found"; // echo no users JSON echo json_encode($response); } }else { // required field is missing $response["success"] = 0; $response["message"] = "Error Please try again"; // echoing JSON response echo json_encode($response); } ?>

{{1}}

答案 1 :(得分:1)

不是100%这是解决方案,但我重新格式化了&amp;看了你的代码。这就是我所拥有的。首先,你有(int)page*10;,这在上下文中毫无意义。所以我将其更改为(int) $Page*10;,但这可能只是intval($Page*10);。此外,您使用的mysql_num_rows应该是mysqli_num_rows。所以也做了调整。

$response=array();


if (!empty($_GET["page"])) {
    $Page = $_GET['page'];
    // $limit= intval($Page*10); // Maybe this would work as well.
    $limit= (int) $Page*10;

    $db = mysqli_connect('127.0.0.1', 'root', '', 'CampusList') or die(mysqli_error());
    // get all products from products table
    $result = mysqli_query($db, "SELECT * FROM post order by post_date desc limit $limit") or die(mysqli_error());


    if (mysqli_num_rows($result) > 0) {
        // looping through all results
        // products node
        $response["post"] = array();

        while ($row = mysql_fetch_array($result)) {
            // temp user array
            $post = array();
            $post["post_id"] = $row["post_id"];
            $post["post_name"] = $row["post_name"];
            $post["post_creator"] = $row["post_creator"];
            $post["post_type"] = $row["post_type"];
            $post["post_school_id"] = $row["post_school_id"];
            $post["post_content"] = $row["post_content"];
            $post["price"] = $row["price"];
            $post["post_date"] = $row["post_date"];
            $post["post_class_id"] = $row["post_class_id"];
            $post["post_flagged"] = $row["post_flagged"];

            // push single product into final response array
            array_push($response["post"], $post);
        }
        // success
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);
    }
    else {
        // no products found
        $response["success"] = 0;
        $response["message"] = "No Posts found";

        // echo no users JSON
        echo json_encode($response);
    }
}
else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Error Please try again";

    // echoing JSON response
    echo json_encode($response);
}