选择最新行的主要麻烦

时间:2011-02-04 21:08:03

标签: php mysql variables select

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

例如:

$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);
    }


    }
}

提前致谢

1 个答案:

答案 0 :(得分:1)

如果您想在按行提交数据库时对其进行排序,则需要在表中添加ID字段(如果尚未添加)。

这可能是向表中添加名为id的字段,将其设置为索引并使用auto_increment的最简单方法。然后,您只需按ID订购行:

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

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