This is an example I have grabbed from W3School, and for the purpose of my question, I have modified it and put in the code here:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM example") 
or die(mysql_error());  

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	
	echo $row['name'] . " " . $row['age'] . "<br />"; 
} 

?>

Does anyone know how I can print out the row number of each entry here. Say, if the mysql_num_rows() of the query is 3, then it would print something out like

1 David 24
2 Sam 25
3 Seth 20

How can I do that?
Thanks for your help.

Member Avatar for rajarajan2017
$count=0;
while($row = mysql_fetch_array( $result )) {
	$count=$count+1;
	echo $count . " " . $row['name'] . " " . $row['age'] . "<br />"; 
}
$count=0;
while($row = mysql_fetch_array( $result )) {
	$count=$count+1;
	echo $count . " " . $row['name'] . " " . $row['age'] . "<br />"; 
}

So, there is no SQL statement that is going to work what I want here?
Just want to make sure.

Member Avatar for rajarajan2017

No there is an option to get an rowid in sql. But I am not sure about the syntax.

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.