0

Very good afternoon everyone, I hope you can help me with this question. I have the following login code which works fine, but I am looking to create an associative array for a variable, in this case for the variable $code, I found on the internet that the way to do it is with: $row = mysqli_fetch_assoc($sql); and $_SESSION['code'] = $row['code']; but in my case this does not work.

This is my login code:

<?php

    if(empty($email_err) && empty($password_err)){
        
        $sql = "SELECT id, code, email, cel, passw FROM members WHERE email = ? ";

        if($stmt = mysqli_prepare($link, $sql)){

            mysqli_stmt_bind_param($stmt, "s", $param_email);
            
            $param_email = $email;
            
            if(mysqli_stmt_execute($stmt)){
                mysqli_stmt_store_result($stmt);
                
            }
              
            if(mysqli_stmt_num_rows($stmt) == 1){
                
                mysqli_stmt_bind_result($stmt, $id, $code, $email, $hashed_password);
                 
        
                if(mysqli_stmt_fetch($stmt)){

                    if(password_verify($password, $hashed_password)){
                        session_start();

                        $_SESSION['loggedin'] = true;
                        $_SESSION['id'] = $id;
                        $_SESSION['code'] = $code;
                        $_SESSION['email'] = $email;
                        
                         $active = "Active";
                         $sql2 = mysqli_query($link, "UPDATE members SET active = '{$active}' WHERE code = {$_SESSION['code']}");
                      
                    
                        
                        header("location: ../index.php");
                        
                    }else{
                        $password_err = "ERROR";
                    }
                    
                } 
            }else{
                    $email_err = "ERROR";
                }
            
        }else{
                    echo "ERROR";
                }
    }
    
    mysqli_close($link);


?>

In index.php:

<?php echo $row['code']; ?>

Something I tried was the following:

  if(mysqli_stmt_fetch($stmt)){
    $result = mysqli_stmt_get_result($stmt); 
    $row = mysqli_fetch_assoc($result);
    ...

    $_SESSION['code'] = $row['code'];

However this did not work.

0 Answers0