简体   繁体   English

MYSQL> PHP-与函数相关的查询麻烦,该函数检查表是否包含一行用户详细信息

[英]MYSQL>PHP - Trouble with a query relating to function that checks if a table contains a row of user details

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. 第一个表是users表。 It contains the first set of specific user information. 它包含第一组特定的用户信息。

The last table is the listings table. 最后一个表是listings表。 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. 我基本上已经修改了原始代码,以包括新的listings表,因此麻烦就出在该任务内。 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. 旧代码基本上在users表中计算结果数,如果结果大于0,则该函数返回true,否则返回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. 这将根据users表中存储的信息返回0或1的计数。

This is how I have reworked the query to include a count of the additional listings table: 这是我重新处理查询以包括其他listings的计数的方式:

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. 这返回了两行的结果集,第一行与users表有关,第二行与listings表有关。 Then a column called COUNT (*) that contained the result count. 然后,一个名为COUNT (*)的列包含结果计数。 This is the result set that I see within php myadmin. 这是我在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. 我试过这个: if (($row[0] > 0) || ($row[0] > 0))但是$row上的var dump显示只有第一行( users表数)被添加到数组。

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: 因此,我尝试仅选择结果的id列,如下所示:

...
`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! 但是无论哪种情况,我经过数小时的反复试验都没有给我带来成功。因此,我决定寻求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. 由于我刚开始涉足PHP和mysql,因此如果您想提供一些代码来解释您的建议或有关此问题的输入,那么它很可能会帮助我更好地理解答案。

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 ). 当最后一个表中存在记录(否则为0 )时,它将始终返回一行,其中一列为1

Do not use count to check whether some specific record/-s exist/-s in table, it's usually slower than simple exists . 不要使用count来检查表中是否存在某些特定记录,通常比简单exists慢。

Looks like you're going to get two rows in the result no matter what. 看起来无论如何您都会在结果中得到两行。 Try this: 尝试这个:

$sql = "SELECT id,password
        FROM users
        WHERE id = '$accNum' AND password = '$password'

        UNION

        SELECT id,password
        FROM listings
        WHERE id = '$accNum' AND password = '$password'
";

Now you can just check mysql_num_rows() to see if there's a match in either of the tables. 现在,您只需检查mysql_num_rows()来查看两个表中是否存在匹配项。

There are a couple of ways to go about this; 有两种方法可以解决此问题; if we are to stick with the approach you started with; 如果我们坚持您最初的做法; you can simplify the query to: 您可以将查询简化为:

$sql = "SELECT COUNT(1) FROM users
          WHERE id = '$accNum'
          AND PASSWORD = '$password'
        UNION (SELECT COUNT(1) FROM listings
          WHERE id = '$accNum'
          AND PASSWORD = '$password')";

The reason you are only seeing one result, is because thats the way mysql_fetch_array() works, try doing this to get all results: 之所以只看到一个结果,是因为这就是mysql_fetch_array()的工作方式,请尝试这样做以获得所有结果:

while ($row = mysql_fetch_array($result)) {
  $data[] = $row;  
}

var_dump($data);

Now you should have both values in there to validate with your conditional statements. 现在,您应该在其中具有两个值,以便可以使用条件语句进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM