PHP undefined index error

Asked 2 years ago, Updated 2 years ago, 46 views

I have created columns such as name and pw in the MySQL user table.
Undefined index:name
when running PHP below I get the error, but I don't know the cause.
The pw has been correctly determined.

$ps=$db->prepare("SELECT pw FROM user WHERE email=:u_mail");
$ps->bindParam(":u_mail", $m);
$ps->execute();
if($ps->rowCount()>0){
   $r = $ps-> fetch (PDO::FETCH_ASSOC);
   if($r['pw']===md5($p)){
       $_SESSION['user'] = $r['name'];

php mysql

2022-09-30 19:12

2 Answers

I solved myself.
SELECT pw FROM user WHERE email=:u_mail

not
SELECT pw, name FROM user WHERE email=:u_mail
It would have been fine if I did.


2022-09-30 19:12

Hi, nice to meet you.I don't know if I can help you, but at this stage, I'll write something I'm curious about.

Undefined index:name
I think shows that there is no key name for the $r associative array.

$ps=$db->prepare("SELECT pw FROM user WHERE email=:u_mail");
$ps->bindParam(":u_mail", $m);
$ps->execute();
if($ps->rowCount()>0){
$r = $ps-> fetch (PDO::FETCH_ASSOC);
// See if there is a $r key here
print var_export($r);

if($r['pw']===md5($p)){
$_SESSION['user'] = $r['name'];

Whether the print is displayed on the screen depends on how it is implemented.
When the var_export() result is displayed in the standard output,
Find the return value for $ps->fetch(PDO::FETCH_ASSOC);

Try it.


2022-09-30 19:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.