【问题标题】:major trouble matching selecting most current row匹配选择最新行的主要问题
【发布时间】:2011-02-04 21:08:03
【问题描述】:

我在选择数据库中最新/匹配的行时遇到了很大问题。

例如:

$query = "SELECT * FROM `Password_Reset`";
    $request = mysql_query($query,$connection) or die(mysql_error());
    $result = mysql_fetch_array($request);

$result['token'] 在每次运行查询时都会占用第一行。

我很确定这与我的选择查询不够具体有关,但我还没有找到匹配它的方法。

更具体。这是整个查询:

$query = "SELECT * FROM `Password_Reset`";
    $request = mysql_query($query,$connection) or die(mysql_error());
    $result = mysql_fetch_array($request);

    $token = $result['token'];

    $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";
    $rand = str_shuffle($alpha);
    $salt = substr($rand,0,40);
    $hashed_password = sha1($salt . $_POST['Password']);
    $user_email = $result['email'];





       // these should match, and do so every where else on my other pages and in the db, I'm just not able to pull the corresponding $token. its taking the $token in the first row every time.
    if($get_token == $token) {
    header("Location: http://www.cysticlife.org/index.php");    
    exit;
    }else{

    if(empty($_POST['Password'])) {
        $valid = false;
        $error_msgs[] = 'Whoops! You must enter a password.';
    }

    if($_POST['Password'] != $_POST['passwordConfirm'] || empty($_POST['Password'])) {
        $valid = false;
        $error_msgs[] = "Your password entries didn't match...was there a typo?";
    }

    if($valid) {
        $query = "UPDATE `cysticUsers` SET `encrypted_password` = '$hashed_password' WHERE `Email` = '$user_email'";

        mysql_query($query,$connection);
    }


    }
}

提前致谢

【问题讨论】:

    标签: php mysql variables select


    【解决方案1】:

    如果您想按提交到数据库的时间对行进行排序,则需要在表中添加一个 ID 字段(如果您还没有的话)。

    这可能是向表中添加一个名为id 的字段、将其设置为索引并使用auto_increment 的最简单方法。然后,您可以简单地按 ID 对行进行排序:

    SELECT * FROM `Password_Reset` ORDER BY `id` ASC

    顺便说一句,如果您只需要第一行,最好在查询中添加LIMIT 1,以获得更好的性能。

    【讨论】:

    • 我确实有一个自动增量 ID 行。我很确定这与此有关。只是一个新手,正在寻找编写它的逻辑语法。非常感谢
    • 如果您仍然有兴趣提供帮助,那并不能解决问题。它似乎仍在选择第一行。它没有选择相关的 $token
    • 哦,你想得到与某个标记匹配的第一行吗?然后您需要使用 WHERE 子句(在 ORDER BY 之前),例如 WHERE token = '$token'
    • 好酷。这是有道理的,但是在通过 $result['token'] 进行查询之后定义 $token 可以吗?
    • 是的,我想我试过了,因为我在页面加载后打印 $token,当我添加 WHERE token = '$token' 时,它会产生 $token 空而不是让它成为原始问题...只有数据库中的第一行:/
    猜你喜欢
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多