Mysql data is not fetching more than one

I am using this code to pull out data from database. But the $bathc_no variable is not fetching more than 1 row.

Suppose, I have 3 rows in my mysql, Where the $batch_no for the 1st row is “13”, 2nd row is “12” and for the 3rd is “15”.

I put whatever value for $profile_id varialble, the result(only $batch_no) always comes for 1st row, that is 13. Rest of the code($student_name, $sex, $birth_date) is working fine with the variour $profile_id value but only the $batch_no.

I need various $batch_no for various $profile_id.

Please help me.

This is my code

$profile_pull = mysql_query("SELECT * FROM student_profiles WHERE id='$profile_id'");
$profile_count = mysql_num_rows($profile_pull);
if ($profile_count > 0 ) {
	while ($row = mysql_fetch_array($profile_pull)) {
		$student_name = mysql_prep(ucwords($row["student_name"]));
		$batch_no = $row["batch_no"];
		$sex = ucwords($row["sex_of_the_student"]);
		$birth_date = $row["birth_date"];
	}
}

Are you trying to assign single values to variables or multiple values to what should be an array?

Here, I am getting profile Id from $_SESSION[‘id’].

So, When I logged in as different user, I am getting the same $batch_no though the value of $batch_no in the database is different.

First some offtopic remarks (well not really offtopic, but outside the scope of your question):

  1. please use mysqli_ (or pdo), the mysql_ API is deprecated and will be removed in the future
  2. if you need only 1 row from the database, there’s no need to do a while loop (unless you expect to get more rows and only want the last one)

If you are sure about your database content, and all other data from the database is correct, then you’re changing the batch_no somewhere else in your code.
But, to be sure, add the line in red in your code and see what it displays:


$profile_pull = mysql_query("SELECT * FROM student_profiles WHERE id='$profile_id'");
$profile_count = mysql_num_rows($profile_pull);
if ($profile_count > 0 ) {
    while ($row = mysql_fetch_array($profile_pull)) {
[COLOR="#FF0000"][B]        print_r($row);  // this will display the contect of the row; delete this line after you've finished debugging[/B][/COLOR]
        $student_name = mysql_prep(ucwords($row["student_name"]));
        $batch_no = $row["batch_no"];
        $sex = ucwords($row["sex_of_the_student"]);
        $birth_date = $row["birth_date"];
    }
}