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 6816757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:57:51+00:00 2026-05-26T20:57:51+00:00

I have table of users called users and log table called ulog . Every

  • 0

I have table of users called users and log table called ulog. Every time when user signs in, the page adds new record into log table.

To prevent errors, before sign-in, I’m checking if user already signed in or not (I know that it’s not so important, there are other methods for this purpose: for ex, I can prevent it by checking for cookies or session too, but it’s interesting to me how can I do it with my idea).

The problem is code works perfectly if there is at least 1 row in ulog table about the user who currently trying to sign-in. If user wants to sign-in for the first time, the problem occurs: The code “flies over” the if ($stmt->num_rows > 0) { statements and selects else. (because num_rows returns 0)

How can I modify my query to handle in both situations: when user logs in for the first time, and for the second, third… time?

My php side sign-in code looks like that.

  $stmt = $db->prepare("SELECT u.id, u.fname, u.lname, u.mname, u.level, u.pass, u.salt, u.approved, u.ban, u2.logged_in FROM `users` AS u, `ulog` AS u2 WHERE u.email=? AND u2.user_id=u.id") or die($db->error);
$stmt->bind_param("s", $email) or die($stmt->error);
$stmt->execute() or die($stmt->error);
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $fname, $lname, $mname, $level, $db_pass, $salt, $approved, $ban, $logged_in) or die($stmt->error);
$stmt->fetch() or die($stmt->error);
if ($logged_in == 0) {
}
else die("You're already logged in");
}
else die("There is no such email in db");
  • 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-05-26T20:57:51+00:00Added an answer on May 26, 2026 at 8:57 pm

    Trying doing a left outer join of the log table. This will return null for the first time they try and log in, because there is nothing in the log table for them. You will also need to do null checking on $logged_in, as this is a case. The easier thing to do would be to check for 1 in the $logged_in var to validate that they are logged in, as anything other than 1 would mean they were not logged in.

    Here is your first line redone:

    $sql['userLoggedIn'] = "
        SELECT u.id,    u.fname,    u.lname,    u.mname,    u.level,    
               u.pass,  u.salt,     u.approved, u.ban,      u2.logged_in
        FROM `users` AS u
        LEFT OUTER JOIN `ulog` AS u2
            ON u2.user_id = u.user_id
        WHERE u.email = ?";
    
    $stmt = $db->prepare($sql['userLoggedIn']) or die($db->error);
    

    Try that out, see if it does what you need. Left outer joins basically state Show me AT LEAST everything that comes back in the FROM table(s). if there is anything in the LEFT OUTER JOIN table(s), tack that on too

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called Users ( class User < ActiveRecord::Base ) and a
i have table called users , if i want to delete some user (User
I have a table called Users in my database. Let's assume that User has
I have a table called users and each user can belong to one group,
I have a table called UserPermissions with a FK to the users table by
I have a table called USERS that has a foreign key to the table
I have a table of users called Users And a view called UsersActive that
I have a log table in my database which holds all user actions. In
I have a mysql table with a row called log. I need to update
I have a mysql table called users with the following fields: username - password

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.