Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8462101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:04:31+00:00 2026-06-10T14:04:31+00:00

I just started learning mysqli last night and am currently having an issue with

  • 0

I just started learning mysqli last night and am currently having an issue with a function I created. The function should log in the user. However, when I type in an existing username with the authentic or a made up password, the login page reloads displaying the $user_id. I’m at a lost at what is wrong. I didn’t have this problem when I had mysql.

/** 
 * Returns FALSE, if no valid user found
 * Returns user_id of matching user with $username and $password
 */
function login ($mysqli, $username, $password) {

    // not required at all
    // $user_id = user_id_from_username($mysqli, $username);

    // initialize, in case we do not get a mysqli-statement
    $userID = FALSE;
    $password = md5($password);
    $stmt = $mysqli->prepare(
                     "SELECT `user_id`          "
                   . "  FROM `users`            "
                   . " WHERE ( `username` = ? ) "
                   . "   AND ( `password` = ? ) "
            );

    if ( $stmt ) {
        $stmt->bind_param('ss', $username, $password);  
        $stmt->execute();
        $stmt->bind_result($userID);
        if ( TRUE !== $stmt->fetch()) {
            $userID = FALSE;
        }
    }
    $stmt->close();
    return $userID; 
}

And here is when I call the function login in the login page. $mysqli is the variable containing the connection to the database.

// Now, needs to check against FALSE to work [changed by @SteAp]

//   var_dump( $login ); returns with int(1) 
//   and this is what I want, the integer 1

//Sends me to start.php but start.php does not recognize 
//the variable $_SESSION['user_id']
if ( FALSE === ($login = login($mysqli, $username, $password)) ) {  
  $errors[] = 'That username/password combination is incorrect';
} else {
  $_SESSION['user_id'] = $login;
  header('Location: start.php');
  exit();
}

if (empty($errors) === false) {
  echo '<div>'. output_errors($errors) . '</div>';
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:04:33+00:00Added an answer on June 10, 2026 at 2:04 pm

    Return the user info, not the count:

    $stmt = $mysqli->prepare("SELECT `user_id` FROM `users` WHERE `username` = ?"; 
    $stmt->bind_param("s", $_SESSION['username']);
    $result = $stmt->get_result();
    $user_info = $result->fetch_assoc();
    

    Now, $user_info if either FALSE or the found record. So you can get the id from $user_info['id'] and any other user data respectively.

    And, certainly, you need to start a session, before any assignment to $_SESSION gets passed along. Example from PHP manual:

    page1.php

    session_start();
    
    echo 'Welcome to page #1';
    
    $_SESSION['favcolor'] = 'green';
    $_SESSION['animal']   = 'cat';
    $_SESSION['time']     = time();
    
    // Works if session cookie was accepted
    echo '<br /><a href="page2.php">page 2</a>';
    

    page2.php

    session_start();
    
    echo 'Welcome to page #2<br />';
    
    echo $_SESSION['favcolor']; // green
    echo $_SESSION['animal'];   // cat
    echo date('Y m d H:i:s', $_SESSION['time']);
    
    // You may want to use SID here, like we did in page1.php
    echo '<br /><a href="page1.php">page 1</a>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with mysqli and prepared statements. I've just started learning mysqli an
I just started learning C++ (coming from Java ) and am having some serious
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I just started learning C++ and am currently using codeblocks. I want to write
I have just started learning MySQL and am having trouble extracting matching flights from
I just started learning JS the other day and am (of course) having some
I just started learning mysqli (20 minutes ago) and I'm confused on something. In
I'm learning Java and just started playing around with mysql and am having a
I just started learning php last week and I am trying to populate values
Just started learning python (3.2) and have a question. I have created a some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.