Home > database >  Hey devs , i'm still trying to learn php but i had 3 errors in 1 file , i couldn't solve i
Hey devs , i'm still trying to learn php but i had 3 errors in 1 file , i couldn't solve i

Time:03-12

Notice: Undefined variable: pdo in C:\wamp\www\ShoppingCart(db)-PHP-MYSQL\home.php on line 3

Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\wamp\www\ShoppingCart(db)-PHP-MYSQL\home.php on line 3

Error: Call to a member function prepare() on null in C:\wamp\www\ShoppingCart(db)-PHP-MYSQL\home.php on line 3

    <?php
// Get the 4 most recently added products
$stmt = $pdo->prepare('SELECT * FROM products ORDER BY date_added DESC LIMIT 4');
$stmt->execute();
$recently_added_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<?=template_header('Home')?>

<div >
    <h2>Gadgets</h2>
    <p>Essential gadgets for everyday use</p>
</div>
<div >
    <h2>Recently Added Products</h2>
    <div >
        <?php foreach ($recently_added_products as $product): ?>
        <a href="index.php?page=product&id=<?=$product['id']?>" >
            <img src="imgs/<?=$product['img']?>" width="200" height="200" alt="<?=$product['name']?>">
            <span ><?=$product['name']?></span>
            <span >
                &dollar;<?=$product['price']?>
                <?php if ($product['rrp'] > 0): ?>
                <span >&dollar;<?=$product['rrp']?></span>
                <?php endif; ?>
            </span>
        </a>
        <?php endforeach; ?>
    </div>
</div>

<?=template_footer()?>

CodePudding user response:

Add This At The Top Of The File:

$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

because it doesn't seem you have defined $pdo.

all errors will get solved.

also don't forget to add your hostname, dbname, user, and pass while initializing the PDO instance.

You Can Learn More About It Here: PHP PDO

CodePudding user response:

You should instantiate a pdo object from the PDO class first, so you can use it.

$pdo = new PDO("mysql:host=localhost;dbname=$database", $user, $password);

CodePudding user response:

    <?php
function pdo_connect_mysql() {
    // Update the details below with your MySQL details
    $DATABASE_HOST = 'localhost';
    $DATABASE_USER = 'root';
    $DATABASE_PASS = '';
    $DATABASE_NAME = 'shoppingcart';
    try {
        return new PDO('mysql:host=' . $DATABASE_HOST . ';dbname=' . $DATABASE_NAME . ';charset=utf8', $DATABASE_USER, $DATABASE_PASS);
    } catch (PDOException $exception) {
        // If there is an error with the connection, stop the script and display the error.
        exit('Failed to connect to database!');
    }
}
// Template header, feel free to customize this
function template_header($title) {
    // Get the amount of items in the shopping cart, this will be displayed in the header.
$num_items_in_cart = isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0;
}
echo <<<EOT
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>title</title>
        <link href="style.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
    </head>
    <body>
        <header>
            <div >
                <h1>Shopping Cart System</h1>
                <nav>
                    <a href="index.php">Home</a>
                    <a href="index.php?page=products">Products</a>
                </nav>
                <div >
                    <a href="index.php?page=cart">
                        <i ></i>
                    </a>
                </div>
            </div>
        </header>
        <main>
EOT;
//<span>$num_items_in_cart</span>//
// Template footer
function template_footer() {
$year = date('Y');
echo <<<EOT
        </main>
        <footer>
            <div >
                <p>&copy; $year, Shopping Cart System</p>
            </div>
        </footer>
        <script src="script.js"></script>
    </body>
</html>
EOT;
}
?>

CodePudding user response:

the error in line 3 solved but i got another error in line 8 which is :

Fatal error: Uncaught Error: Call to undefined function template_header() in C:\wamp\www\ShoppingCart(db)-PHP-MYSQL\home.php on line 8 Error: Call to undefined function template_header() in C:\wamp\www\ShoppingCart(db)-PHP-MYSQL\home.php on line 8

  •  Tags:  
  • php
  • Related