PHP mysql_fetch_array多个表

时间:2014-05-15 00:05:14

标签: php mysql

所以我有下面的代码,我试图部分作为登录脚本运行,部分是为另一个程序收集一些信息。我没有得到任何错误的脚本,我只是没有得到第二个和第三个mysql_fetch_array的任何信息,我读到这是一个常见的问题,但这应该只适用于同一个表。即便如此,我仍遵循建议的建议,并使用mysql_data_seek重置$ result。我也尝试使用不同的$ result和$ row变量,但我仍然没有从这些查询中获取任何数据。有关如何做到这一点的任何想法?

    $result = mysql_query("SELECT * FROM user WHERE username = '$username'");
    $row = mysql_fetch_array($result);
    $salt = $row['salt'];
    $id = $row['id'];
    $usergroup = $row['usergroupid'];
    $mysql_pass = $row['password'];
    $md5_pass = md5($password.$salt);
    mysql_data_seek ($result, 0);
        if($mysql_pass == $md5_pass)
        {
            $result = mysql_query("SELECT teamid FROM tmnt_members WHERE teamid = '$id'");
            $row = mysql_fetch_array($result);
            $team = $row['teamid'];
            $captain = $row['leader'];
            $cocaptain = $row['coleader'];
            mysql_data_seek ($result, 0);
            $result = mysql_query("SELECT * FROM tmnt_teams WHERE teamid = '$team'");
            $row = mysql_fetch_array($result);
            $teamname = $row['teamname'];
        }

2 个答案:

答案 0 :(得分:2)

您在第二个查询中遗漏了coleaderleader字段。你的三个查询可以很简单地写成一个。

SELECT *
FROM user u, tmnt_members m, tmnt_teams t
WHERE u.username = '$username'
AND m.teamid = u.id
AND t.teamid = m.teamid

或者,如果您希望将身份验证作为单独的查询,则可以执行SELECT * FROM user WHERE username = '$username',然后执行以下操作:

SELECT *
FROM tmnt_members m, tmnt_teams t
WHERE m.teamid = '$id'
AND t.teamid = m.teamid

写这篇文章的时候,我注意到你的第二个查询可能有错误。条件teamid = '$id'似乎很奇怪。目前,您正在获取具有该用户ID的团队。这可能不正确,或者如果是,你的数据库结构非常非常奇怪。我想它应该是memberid = '$id'

另请注意,如果没有我的第一段中建议的更正,此查询会要求数据库获取ID为$id的团队的ID。换句话说,如果该查询正确,您可以直接使用$id

此外,做SELECT *不是最好的做法;最好明确枚举所需的所有字段。如果您更改列名或对数据库进行其他一些修改,您的查询可能仍然有效,但可能无法按预期执行。

答案 1 :(得分:0)

我已将您的代码转换为 MySQLi 至少。我在里面引用了我的评论。并尝试清理您的代码。试试这个:

<?php

$con=mysqli_connect("Host","Username","Password","Database"); /* REPLACE NECESSARY DATA INSIDE */

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

$username=mysqli_real_escape_string($con,$_POST['username']); /* ASSUMING $username COMES FROM A POST DATA. JUST REPLACE IF NECESSARY */

$result = mysqli_query($con,"SELECT * FROM user WHERE username = '$username'");
while($row = mysqli_fetch_array($result)){

$salt = mysqli_real_escape_string($con,$row['salt']);
$id = mysqli_real_escape_string($con,$row['id']);
$usergroup = mysqli_real_escape_string($con,$row['usergroupid']);
$mysql_pass=mysqli_real_escape_string($con,$row['password']);
$md5_pass = md5($password.$salt); /* MAKE SURE YOU HAVE $password AND $salt VARIABLES DECLARED ABOVE */

        if($mysql_pass == $md5_pass)
        {
            $result2 = mysqli_query($con,"SELECT teamid, leader, coleader FROM tmnt_members WHERE teamid = '$id'"); /* ADDED leader AND coleader */
            while($row2 = mysqli_fetch_array($result2)){
            $team = mysqli_real_escape_string($con,$row2['teamid']);
            $captain = mysqli_real_escape_string($con,$row2['leader']);
            $cocaptain = mysqli_real_escape_string($con,$row2['coleader']);

                 $result3 = mysqli_query($con,"SELECT * FROM tmnt_teams WHERE teamid = '$team'");
                 while($row3 = mysqli_fetch_array($result3)){
                 $teamname = mysqli_real_escape_string($con,$row3['teamname']);
                 } /* END OF THIRD LOOP */

            } /* END OF SECOND WHILE LOOP */

        } /* END OF IF MYSQL_PASS IS EQUALS TO MD5_PASS */

/* IS THIS WHERE YOU WANT TO PRINT YOUR RESULTS? */
echo $id." ".$usergroup." ".$team." ".$captain." ".$cocaptain. " ".$teamname;

} /* END OF WHILE LOOP */

?>