Answer the question
In order to leave comments, you need to log in
Extracting data from the database through Pdo?
A beginner , wrote registration and authorization in mysql , found out that old stuff and you need to switch to PDO , decided to rewrite the code from mysql to PDO and ran into a problem . During registration, I want to pull the user ID from the database, write it to the session and display this session in the profile.php file, but it doesn't work out for me, nothing is displayed. wardampil , returns an empty array . The code :
$user = $dbh->prepare("SELECT * FROM `users` WHERE `email` = :email ");
$user->execute([
"email" => $email,
]);
$user = $user->fetchAll();
$_SESSION['id'] = $user['id'];
var_dump($_SESSION['id']);
Answer the question
In order to leave comments, you need to log in
Can be done using custom post type, like normal posts where you will upload photos...
maybe there is. Have not seen. It is not difficult to do this, with the help of afc you can
more or less like this
define( "DB_HOST", 'localhost' );
define( 'DB_NAME', '' ); // заполнить
define( 'DB_USER', '' );
define( 'DB_PASSWORD', '' );
define( 'DB_CHARSET', 'utf8mb4' );
$host_db = DB_HOST;
$name_db = DB_NAME;
$user_db = DB_USER;
$pass_db = DB_PASSWORD;
$char_db = DB_CHARSET;
$dsn = "mysql:host=$host_db;dbname=$name_db;charset=$char_db";
$options=[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try{
$dbh = new PDO($dsn,$user_db,$pass_db,$options);
}catch(
PDOException $e){
die("NO CONNECT -- ". $e->getMessage());
}
function db_user($email){
global $dbh;
$user = $dbh->prepare("SELECT * FROM `users` WHERE `email` = :email ");
$user->execute([
"email" => $email,
]);
$result = $user->fetchAll();
return (count($result) > 0) ? $result : []; // <-- многомерный массив! , т.к. возможны юзеры с одинаковой почтой
}
$email = ''; // искомая почта
$users = db_user($email);
$_SESSION['id'] = isset($users[0]['id']) ? $users[0]['id'] : '0'; // в сессию id первого попавшегося или 0
echo var_export($users,1);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question