Snippets Collections
<?php

    class AcessoBD{

        private $conn;
        private $host;
        private $dbname;
        private $username;
        private $password;

        public function __construct($dbname){
            $this ->host="localhost";
            $this ->dbname=$dbname;
            $this ->username="root";
            $this ->password="";
            $this ->setConn();
        }

        private function setConn(){
            try {
                $this ->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->dbname, $this->username, $this->password);
                $this ->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch(PDOException $e) {
                echo 'ERROR: ' . $e->getMessage();
            }
        }

        public function select($sql,$parametros){
            $stmt = $this ->conn->prepare($sql);
            $stmt->execute($parametros);
            return $stmt ->fetchAll();
        }
    }
 
 ?>
$host = '127.0.0.1';
$db   = 'test';
$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());
}
<?php

//The example selects and prints a specific row.

$dsn = "mysql:host=localhost;dbname=mydb";
$user = "user12";
$passwd = "12user";

$pdo = new PDO($dsn, $user, $passwd);

$id = 12;

//This time we use named placeholder (:id) and bindParam.
$stm = $pdo->prepare("SELECT * FROM countries WHERE id = :id");
$stm->bindParam(":id", $id, PDO::PARAM_INT);
$stm->execute();

$row = $stm->fetch(PDO::FETCH_ASSOC);

echo "Id: " . $row['id'] . PHP_EOL;
echo "Name: " . $row['name'] . PHP_EOL;
echo "Population: " . $row['population'] . PHP_EOL;

?>
<?php

//In the example, we use bindValue to create a parameterized query. We use question mark placeholder.

$dsn = "mysql:host=localhost;dbname=mydb";
$user = "root";
$passwd = "andrea";

$pdo = new PDO($dsn, $user, $passwd);


$id = 12;

//Say this input comes from a user.
$stm = $pdo->prepare("SELECT * FROM countries WHERE id = ?");
$stm->bindValue(1, $id);
$stm->execute();

//The select statement fetches a specific row from the table. We bind the value with bindValue to a question mark placeholder.
$row = $stm->fetch(PDO::FETCH_ASSOC);

echo "Id: " . $row['id'] . PHP_EOL;
echo "Name: " . $row['name'] . PHP_EOL;
echo "Population: " . $row['population'] . PHP_EOL;

?>
<?php

//In this example, we fetch data as an associative array.

$dsn = "mysql:host=localhost;dbname=mydb";
$user = "user12";
$passwd = "12user";

$pdo = new PDO($dsn, $user, $passwd);

$stm = $pdo->query("SELECT * FROM countries");

//In the fetchAll method, we use the PDO::FETCH_ASSOC style.
$rows = $stm->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $row) {

    printf("{$row['id']} {$row['name']} {$row['population']}\n");
}

?>
<?php

//In this code example, we get data in an indexed array.

$dsn = "mysql:host=localhost;dbname=mydb";
$user = "user12";
$passwd = "12user";

$pdo = new PDO($dsn, $user, $passwd);

//We select all data from the countries table.
$stm = $pdo->query("SELECT * FROM countries");

//We pass the PDO:FETCH_NUM style to the fetchAll method.
$rows = $stm->fetchAll(PDO::FETCH_NUM);

//We go over the $rows array and print the fields. The fields are accessed via array indexes.
foreach($rows as $row) {
    printf("$row[0] $row[1] $row[2]\n");
}

?>
<?php
//The code example deletes three rows. It prints the number of affected rows.


$dsn = "mysql:host=localhost;dbname=mydb";
$user = "user12";
$passwd = "12user";

$pdo = new PDO($dsn, $user, $passwd);

$id = 12;

//In this SQL statement, we delete rows with ids 1, 2, and 3. The number of deleted rows is stored in the $nrows variable.
$nrows = $pdo->exec("DELETE FROM countries WHERE id IN (1, 2, 3)");

//We print the number of deleted rows.
echo "The statement affected $nrows rows\n";

?>
<?php

//These variables are used to create a connection string to the database. The dsn is the Data Source Name, which contains the information required to connect to the database.
$dsn = "mysql:host=localhost;dbname=mydb";
$user = "user12";
$passwd = "12user";

//A new PDO object is created. We pass the constructor the data source name and the user name and password. The PDO class represents a connection between PHP and a database server.
$pdo = new PDO($dsn, $user, $passwd);

//The query method executes an SQL statement in a single function call. It returns the result set.
$stm = $pdo->query("SELECT VERSION()");

//The PDO statement's fetch method fetches the next row from a result set. In our case it is a version of MySQL.
$version = $stm->fetch();

//The $version is an array; we get its first value.
echo $version[0] . PHP_EOL;

?>
star

Fri Sep 30 2022 17:28:10 GMT+0000 (UTC)

#php #pdo
star

Wed Sep 14 2022 13:34:16 GMT+0000 (UTC) https://phpdelusions.net/pdo

#mysql #bd #pdo #php
star

Thu Sep 08 2022 09:32:40 GMT+0000 (UTC) https://zetcode.com/php/pdo/

#php #mysql #pdo
star

Thu Sep 08 2022 09:30:39 GMT+0000 (UTC) https://zetcode.com/php/pdo/

#php #mysql #pdo
star

Thu Sep 08 2022 09:24:44 GMT+0000 (UTC) https://zetcode.com/php/pdo/

#php #mysql #pdo
star

Thu Sep 08 2022 09:22:09 GMT+0000 (UTC) https://zetcode.com/php/pdo/

#php #mysql #pdo
star

Thu Sep 08 2022 09:17:16 GMT+0000 (UTC) https://zetcode.com/php/pdo/

#php #mysql #pdo
star

Thu Sep 08 2022 09:13:05 GMT+0000 (UTC) https://zetcode.com/php/pdo/

#php #mysql #pdo

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension