Jump to content

PDO not working in my function (error: "Undefined variable '$pdo'.intelephense(1008)")


oz11
Go to solution Solved by requinix,

Recommended Posts

$host = '127.0.0.1';
$db   = 'news';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}


function banCheck($id) 
{
    $stmt = $pdo->prepare("SELECT suspended FROM `users` WHERE user_id=? LIMIT 1");
    $stmt->execute([$id]); 
    $check = $stmt->fetch();
    if($check['suspended'] == 1){ 
        //header("Location: banned.php"); 
        echo "banned";
    }  else {
        echo "not banned";
    }
}
banCheck("4");

I'm not sure what's happening or no happening at the moment.

 

"Undefined variable '$pdo'.intelephense(1008) "

- line 24

Screenshot from 2022-07-22 16-05-58.png

Edited by oz11
added screen shot
Link to comment
Share on other sites

I updated the code to...

<?php

$host = '127.0.0.1';
$db   = 'news';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}


function banCheck($pdo, $id) 
{
    
    $stmt = $pdo->prepare("SELECT suspended FROM `users` WHERE user_id=? LIMIT 1");
    $stmt->execute([$id]); 
    $check = $stmt->fetch();
    if($check['suspended'] == 1){ 
        //header("Location: banned.php"); 
        echo "banned";
    }  else {
        echo "not banned";
    }
}
banCheck($pdo, 14);


?>

Thanks requinix pal :)

Link to comment
Share on other sites

23 minutes ago, oz11 said:
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

where are you kids getting this from?

there's no good reason to catch a connection error, only to throw a new exception consisting of only the error message and code. this leaves off the filename and line where the error occurred, hindering debugging.  for a connection error, just let php catch and handle the exception, i.e. remove all the try/catch logic from that code.

the only case where your code should catch and handle a database exception is when inserting/updating duplicate or out of range visitor submitted data. the catch logic would test the error number and if it is for something that the visitor can correct, setup a message telling them exactly what was wrong with the data that they submitted, so that they can potentially submit a different value that will succeed. for all other error numbers, just re-throw the existing exception.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.