Search code examples
phpmysqljsonparse-error

Connecting to MySQL database using PHP


can anyone please help me with this, whats wrong with this php code ?

according to "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);
}
?>

Solution

  • https://www.php.net/mysql_query

    try something like this:

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