I wanted to know the difference between the presence and absence of the first condition of the code below

Asked

Viewed 29 times

0

I have a code that I picked up somewhere, some time ago, that I use to make connections with my database and it always worked well. I tried, then, to remove a piece of code that I didn’t understand and, to my surprise, it kept working.. or so I thought. Throughout the project, I was obliged to use the lastInsertId() function, from PHP, but always returned 0, regardless of the command I tried to execute before in the database. I spent a lot of time understanding that what was missing for the code to work as I wanted, returning the value of the last id inserted in the table of that database, was the line of code that I had removed, which is a condition.

Below, the code that works:

class MySql{
private static $pdo;

protected function MySql(){
    if (self::$pdo == null) {
        try {
            self::$pdo = new PDO('mysql:host='.BD['host'].';dbname='.BD['name'],BD['user'],BD['password'],array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            self::$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            
        } catch (Exception $e) {
            echo '<h2>Erro ao conectar</h2>';
        }
    }
    return self::$pdo;
}

}

Now, what doesn’t work:

class MySql{
private static $pdo;

protected function MySql(){
        try {
            self::$pdo = new PDO('mysql:host='.BD['host'].';dbname='.BD['name'],BD['user'],BD['password'],array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            self::$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
            
        } catch (Exception $e) {
            echo '<h2>Erro ao conectar</h2>';
        }
    return self::$pdo;
}

}

Now, what I used to try to get the last id:

$sql = $this->MySql()->prepare("$query");
$sql->execute($parametros);
$ultimo_id = $this->MySql()->lastInsertId();
        

Where $query is an insertion query and $parameters is an array containing the values - obviously, the table must have a primary key, which I always call 'id'. The first code works well, while the second always returns 0. The only difference between them is the if condition. I wanted to understand what the difference is between the two and why this difference happens. I would appreciate it if someone could explain to me.

  • 1

    Your question is not objective enough. Arrange it like this: 1) What I hope will happen 2) What happens 3) My question of why it happens Whish in the title. Try to put the title as technical as possible. Talk about functions, classes, variables, and not condition A or B. I guarantee we will be able to help better!

  • if (self::$pdo == null) { this if is basically to ensure that there is only one instance of the variable $pdo, that is static by the way and it makes perfect sense, ie only instance once if it is null, to avoid connecting to the bank always

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.