使用PHP连接到MySQL数据库

时间:2014-04-11 13:16:53

标签: php mysql json parse-error

任何人都可以帮我解决这个问题,这个php代码有什么问题吗?

根据" http://phpcodechecker.com/" :

Parse error: syntax error, unexpected '$response' (T_VARIABLE) in your code on line 7 $response["products"] = array();

get_all_products.php

<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("SELECT *FROM products") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
    $response["products"] = array();
 
    while ($row = mysql_fetch_array($result)){
        $product = array();
        $product["pid"] = $row["pid"];
        $product["name"] = $row["name"];
        $product["price"] = $row["price"];
        $product["created_at"] = $row["created_at"];
        $product["updated_at"] = $row["updated_at"];
        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";
    echo json_encode($response);
}
?>

1 个答案:

答案 0 :(得分:1)

http://ua2.php.net/mysql_query

尝试这样的事情:

$db = new DB_CONNECT();
$result = mysql_query("SELECT * FROM products", $db);