8

Localhost returns an error instead of my website project displaying

 1 year ago
source link: https://www.codeproject.com/Questions/5333140/Localhost-returns-an-error-instead-of-my-website-p
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Greetings,

I am currently facing a problem that has persisted time and time again, I'm trying to initialize my php website to fetch data from my database and display it on the website, however when I try to run it I am met with the following fatal error:

Copy Code
Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROMproduct' at line 1 in E:\XAMPP\htdocs\youkneadedmebakedgoods\youkneadedme\database\Product.php:16 Stack trace: #0 E:\XAMPP\htdocs\youkneadedmebakedgoods\youkneadedme\database\Product.php(16): mysqli->query('SELECT * FROMpr...') #1 E:\XAMPP\htdocs\youkneadedmebakedgoods\youkneadedme\functions.php(16): Product->getData() #2 E:\XAMPP\htdocs\youkneadedmebakedgoods\youkneadedme\header.php(24): require('E:\\XAMPP\\htdocs...') #3 E:\XAMPP\htdocs\youkneadedmebakedgoods\youkneadedme\index.php(3): include('E:\\XAMPP\\htdocs...') #4 {main} thrown in E:\XAMPP\htdocs\youkneadedmebakedgoods\youkneadedme\database\Product.php on line 16



Below is my code:

Expand ▼   Copy Code
<?php

//Use to fetch product data
class Product
{
    public $db = null;

    public function __construct(DBController $db)
    {
        if (!isset($db->con)) return null;
        $this->db = $db;
    }

    //fetch product data using getData method
    public function getData($table = 'product'){
        $result = $this->db->con->query("SELECT * FROM{$table}");

        $resultArray = array();

        //fetches product data 1 by 1
        while(mysqli_fetch_array($item = $result, MYSQLI_ASSOC)){ /*<-- resulttype: */
            $resultArray[] = $item;
        }

        return $resultArray;
    }
}



The function that I used to try and print the database data also calls an error:

Copy Code
<?php

//require MySQL Connection
require('database/DBController.php');

//require Product Class
require('database/Product.php');

//DBController object
$db = new DBController(); /* <-- can't for the life of me figure out why it calls an error
                           update: fixed it after 2 painful days, turns out I'm just an idiot*/

//Product Object
$product = new Product($db);

print_r($product->getData()); /* <--another error */



What I have tried:

I have tried browsing google and forums for people who might have encountered the same problems as I have as well as looking for different ways to mitigate the problem or at least look for the correct syntax but to no avail.

I apologize for I am completely new to the backend programming scene and wish to be enlightened more. As such, I am asking for your assistance and would be grateful for any help. Thank you


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK