Print consecutive rows of the same column mysqli

1

I tried printing two consecutive rows of the same column with MySQLi, but I only print the same value.

$result = $link->query("SELECT id_papers, name, pen FROM Papers, Pens WHERE id_papers = id_pen_papers"); // la consulta no es exactamente asi pero es para resumir

foreach ($result as $row) { 
        echo "<option>Page ".$row['id_papers']." & ".$row['id_papers']."</option>";
}

Probably with mysql_fetch_array instead of foreach, but I did not succeed.

Solution:

$count = 0;
foreach ($result as $row) { 

  ++$count;
  if($count == 1){ 
      echo "<option>";
  }
  echo $row['id_papers'];
  $count;

  if($count == 1){ 
      echo " & ";
  }

  if ($count == 2){
      echo '</option>';
      $count = 0;
  }
}
    
asked by Menzezzz 04.09.2018 в 14:50
source

2 answers

0

If you want to use foreach, first you have to fetch all the records and put them in an array:

while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
}

For example, before the foreach and then change it like this:

foreach ($rows as $row) { 
    
answered by 04.09.2018 в 14:59
0

Try this way:

if ($result->num_rows > 0) {
  while ($row= mysqli_fetch_array($result)) {
        echo "<option>Page ".$row['id_papers']." & ".$row['id_papers']." </option>";
  }
}else{
  echo = "No hay resultados";
}

I hope you serve, you tell me ...

    
answered by 04.09.2018 в 15:00