3

In my database table there is a column named 'marks' . it contains values like 50,55,67,88,... Now I need to read this values one by one like - first 50, then 55 and so on. How is it possible using php ?

include("db_connect.php"); 
   $result = mysql_query("SELECT * FROM  students   ",$con);
   while($rows = mysql_fetch_array($result))
{
   $mark1=$rows['marks'];//what will do here
   $mark2=$rows['marks']; //should get value 55 and so on
}
3
  • each row has 50,55,67 .. etc ? Commented Sep 18, 2014 at 7:49
  • $array=explode(',',$rows['marks']);print_r($array) Commented Sep 18, 2014 at 7:50
  • 1
    Obligatory mysql_ is deprecated comment. Commented Sep 18, 2014 at 7:52

5 Answers 5

6

If your values are comma separated then explode the field.

http://php.net/manual/en/function.explode.php

include("db_connect.php"); 
$result = mysql_query("SELECT * FROM  students", $con);

while($rows = mysql_fetch_array($result)) {
   $mark=explode(',', $rows['marks']);//what will do here
   foreach($mark as $out) {
      echo $out;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Explode the data from the database. Use the explode function.

Access using indexes

while($rows = mysql_fetch_array($result)) {
  $marks = $row['marks']; //get value of marks from the database       
  $exp = explode("," , $marks); //explode marks data

  $mark1 = $exp[0]; //result is 50
  $mark2 = $exp[1]; //result is 55
  $mark3 = $exp[3]; //result is 67
}

Or loop using foreach

while($rows = mysql_fetch_array($result)) {
  $marks = $row['marks']; //get value of marks from the database
  $exp = explode("," , $marks); //explode marks data

  foreach($exp as $mark) {
    echo $mark;
  }

}

Comments

1

If that row contains ,, the just use explode():

while($rows = mysql_fetch_assoc($result)) {
    $mark1 = explode(',', $rows['marks']); // should contain the comma separated values
    // in array form
    // then loop again
    foreach($mark1 as $mark_piece) {
        // do something here
    }
}

Comments

1

You should use the explode function

$marks = explode(",", $rows['marks']);
foreach($marks as $mark){
  //your code to do something with the mark.
}

Comments

0

N.B. explode() function breaks the string into array, it accepts three arguments, first one is the delimiter that specifies where to break the string, second one is the string that needs splitting, and third one is not mandatory but it tells how many array to return.

$str_to_exploade = 'This,string,needs,some,exploiting';
$explode_string = explode(',', $str_to_exploade);
echo '<pre>';
print_r($explode_string);
echo $explode_string[0].'<br/>';
echo $explode_string[1];

More about explode() go to : http://php.net/manual/en/function.explode.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.