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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:29:20+00:00 2026-05-27T12:29:20+00:00

I am having trouble with a function that checks if a set of user

  • 0

I am having trouble with a function that checks if a set of user entered info (username and password) exists within either of the two possible tables where this information is stored.

The first table is the users table. It contains the first set of specific user information.

The last table is the listings table. It contains the second set of specific user information.

I have basically modified my original code to include the new listings table, and hence the trouble coming from within that task. The old code basically counted the number of results in the users table, if the result was greater than 0, then the function returned true, else false.

Now I have been stuck on the best way to go about adding another table to the query, and function. So I have been playing around with a union.

This was the original query:

SELECT COUNT(*) FROM users
WHERE id='$accNum' AND password='$password'

This returned a count of either 0 or 1 based on the info stored in the users table.

This is how I have reworked the query to include a count of the additional listings table:

SELECT count . *
    FROM (
        SELECT COUNT( * )
        FROM users
        WHERE id = '$accNum'
        AND PASSWORD = '$password'
        UNION (

        SELECT COUNT( * )
        FROM listings
        WHERE id = '$accNum'
        AND PASSWORD = '$password'
        )
    )count

This returned a result set of two rows, the first relating to the users table, and the second relating to the listings table. Then a column called COUNT (*) that contained the result count. This is the result set that I see within php myadmin.

Now this is the function:

function databaseContainsUser($accNum, $password)
{
    include $_SERVER['DOCUMENT_ROOT'] . '/../../includes/db.inc.php';

    $accNum = mysqli_real_escape_string($link, $accNum);
    $password = mysqli_real_escape_string($link, $password);


$sql = "SELECT count . *
        FROM (
            SELECT COUNT( * )
            FROM users
            WHERE id = '$accNum'
            AND PASSWORD = '$password'
            UNION (

            SELECT COUNT( * )
            FROM listings
            WHERE id = '$accNum'
            AND PASSWORD = '$password'
            )
        )count
";

    $result = mysqli_query($link, $sql);
    if (!$result)
    {
        $error = 'Error searching for user.';
        include 'error.html.php';
        exit();
    }

    $row = mysqli_fetch_array($result);

    if ($row[0] > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

The problem that I have, is trying to work out how exactly to check the results to ascertain if the given log in credentials are valid.

I tried this: if (($row[0] > 0) || ($row[0] > 0)) But a var dump on $row showed that only the first row (count of users table) was being added to the array.

So I decided that this was complicated, and a long way to the final result.

So I tried selecting only the id column of the result as in:

...
`COUNT( * )` to `id`
...
$data = mysql_query($sql);
$num_sql = mysql_num_rows($data);
if ($num_sql > 0)
...

But this did not work out for me either.

But in either instance, my hours of trial and error have provided me with no success… So I’ve decided to seek help from the knowledgeable members of Stack Overflow!

So my question is this, what would be a logical way of going about this task? I am looking for any suggestions, or positive input what so ever here.

As I am fairly new to dabbling with PHP and mysql, if you would like to provide some code to explain your suggestions or input on the matter, it would more than likely help me to better understand the answer.

  • 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-27T12:29:21+00:00Added an answer on May 27, 2026 at 12:29 pm

    If you are checking existence only try doing this that way:

    select case when 
        exists (SELECT 1 FROM users WHERE id = '$accNum' AND PASSWORD = '$password') or 
        exists (SELECT 1 FROM listings WHERE id = '$accNum' AND PASSWORD = '$password')
        then 1 else 0 
    end as itDoesExist
    

    It returns always one row with one column with 1 when record exists in at last one table (else 0).

    Do not use count to check whether some specific record/-s exist/-s in table, it’s usually slower than simple exists.

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

Sidebar

Related Questions

I'm having trouble creating a mouseover function with an NSTableView. The idea is that
I'm having trouble creating a global function accessible from within all classes. I receive
I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar
I am having trouble building a Python function that launches TkInter objects, with commands
I'm having trouble replacing a function from a different module with another function and
I'm having trouble with the MyClass::function(); style of calling methods and can't figure out
I'm creating my own dictionary and I am having trouble implementing the TryGetValue function.
Im having some trouble writing a getstring function, this is what I have so
I'm having a bit of trouble with the mktime function. On my production server,
I'm having a bit of trouble iterating and retrieving width() values. $(.MyClass).each( function ()

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.