简体   繁体   中英

PHP mysql_fetch_array multiple tables

So I have the below code that I'm trying to run partly as a login script, and partly to gather some information for another program. I don't get any errors with the script, I just don't get any information from the second and third mysql_fetch_array which I read is a common problem but that should only apply to the same table. Even so, I followed the recommended advice and used mysql_data_seek to reset the $result. I've also tried using different $result and $row variables but I still don't get any data back from those queries. Any thoughts on how I can do this?

    $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'];
        }

You're missing your coleader and leader fields in your second query. Your three queries can be written as one quite simply.

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

Or if you would like to keep the authentication a separate query, you could do SELECT * FROM user WHERE username = '$username' followed by:

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

While writing this, I noticed there probably is an error in your second query. The condition teamid = '$id' seems pretty strange. Currently, you're fetching the team which has an ID that is the one of the user. That can't be correct, or if it is, your database structure is very, very strange. I guess it should be something like memberid = '$id' .

Also notice that without the corrections suggested in my first paragraph, this query is asking the database to fetch the ID of a team which has the ID $id . In other words, you could've just used $id directly if that query was correct.

Moreover, doing a SELECT * isn't the best practice; it's better to enumerate all the fields you want explicitly. If you change your column names or do some other modifications to your database, your query may still work, but may not do what you expect it to do.

I've converted your code to MySQLi at least. I have quoted my comments inside. And did try to clean your code. Try this:

<?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 */

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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