Yohan Blake
Yohan Blake

Reputation: 1318

Looping through a MySQL query in PHP

I have some code that queries my MySQL database. My problem is, if checks only the first row of my table. I know I need to have a for loop, but what I tried out does not work. The working code that checks only the first line is

public function checkPart($aid, $uname) {
    $result = mysql_query("SELECT * FROM part WHERE aid = '$aid'") or die(mysql_error());
    // check for result 
    $no_of_rows = mysql_num_rows($result);

        if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);
            $aiddb = $result['aid'];
            $unamedb = $result['uname'];

            if ($unamedb == $uname) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return mysql_error();
        }

}

What I tried is this:

public function checkPart($aid, $uname) {
    $result = mysql_query("SELECT * FROM part WHERE aid = '$aid'") or die(mysql_error());
    // check for result 
    $no_of_rows = mysql_num_rows($result);
    for($x=1; $x<= $no_of_rows;$x++){
        if ($no_of_rows > 0) {
            $result = mysql_fetch_array($result);
            $aiddb = $result['aid'];
            $unamedb = $result['uname'];

            if ($unamedb == $uname) {
                // user authentication details are correct
                return $result;
            }
        } else {
            // user not found
            return mysql_error();
        }
    }
}

Can someone please help me with correcting my code? I am a beginner so pardon me if the question is too simple.

Upvotes: 2

Views: 552

Answers (2)

Falt4rm
Falt4rm

Reputation: 910

public function checkPart($aid, $uname) 
{
        // It's think it's better to distinct the query itself
        // Therefore u can re-use the code easily.

       $sql = "SELECT * FROM part WHERE aid = \"$aid\"";
       $result = mysql_query($sql);

       // Catch errors
       if (!$result) 
       {
           die('Invalid query: ' . mysql_error());
       }

       // If i'm correct u're using for just to loop the result
       // of fetch_array
      // It's easier like in the example in the man to do

       while ($row = mysql_fetch_array($result))
       {
           // Didn't see where this variable is used but u'll use later i suppose

           $aiddb = $row['aid'];
           $unamedb = $row['uname'];

           if ($unamedb == $uname) 
           {
               // user authentication details are correct
               return $result;
           }
           else
              echo 'User not found \n';
    }
} // checkPart()

I'm pretty new to coding aswell but I suggest u to read the man a lot. It's really Helped me. Btw u should take a look at PDO for database query.

Source :

PHP.net fetch_array

PDO introduction

Upvotes: 1

user1544337
user1544337

Reputation:

The de facto way to do this is with a while loop:

$no_of_rows = mysql_num_rows($result);
if ($no_of_rows == 0) {
    return mysql_error();
} else {
    while ($row = mysql_fetch_assoc($result)) {
        // ... use $row['aid'], $row['uname'], etc.
    }
}

Your own code may work, but you were overwriting $result:

$result = mysql_fetch_array($result);

So, after one iteration of the loop, you had lost the result of the query.

Note: mysql_* functions are deprecated due to security issues, you are advised to learn mysqli_* or PDO instead.

Upvotes: 1

Related Questions