Hello,

I'm trying to get mysql data with a simple select statement, I need to get the number of the row as well.
The table should like this:

1 - Song Name, Artist
2 - Song Name, Artist
3 - Song Name, Artist

The problem is with the code I'm using, the output is coming out like this

12345 Rihanna - Song #1

12345 Bon Jovi - Song #2

12345 Bon Jovi - Song #2

12345 Test - Test #1

This is my code, help is very much appreciated.

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM j2mp6_table");

while($row = mysql_fetch_array($result))
  { ?>

<p class="numblocks num-1">
<? 
for ($i=1; $i<=5; $i++)
  {
  echo "<span>" . $i . "</span>";
  }
?>

<? echo $row['artiste']; ?> - <? echo $row['chanson'];  ?>
</p>
<? 
  //echo $row['artiste'] . " " . $row['chanson'];
  //echo "<br />";
  }


mysql_close($con);
?> 

Can you please verify, if I am understanding your question fully well.

this
12345 Rihanna - Song #1

is roughly the output of this?

echo $row['id']." ".$row['artiste'] ." Song # ".$i;

If so, you don't have to put for loop within the while loop. You can let the $i increment while piggy back ridding on the while loop.. For example,

 $i = 0;

 while($row = mysql_fetch_array($result)){

$i++;

}

As long as the above remain true, $i will continue to increment until row returns false.

Change your while loop with following code:

<?
$cnt = 1;
while($row = mysql_fetch_array($result))
{ ?>
<p class="numblocks num-1">
<?
    echo $cnt.' - '.$row['chanson'].', '.$row['artiste'];
    $cnt++;
?>
</p>
<?
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.