Page 1 of 1

Php mysql database question?

Posted: Tue Aug 16, 2011 1:38 am
by mittul
hello .. i m developing website using PHP and MYSQL - PHPMYADMIN .. and i want to get this kind of output on my website when the admin - user sees using mysql and php .. here is the output which i want ..

--------------------------------------…
OUTPUT
--------------------------------------…
1 name1
2 name2
3 name3
4 name4
5 name5
6 name6
--------------------------------------…

here is my script which i have used ...
--------------------------------------…
mypage.php
----------------------------------

***** PLEASE USE PHP CODE TAGS *****

Code: Select all

<?php

$conn = mysql_connect('localhost','root','') or die('could not connect to remote server');
mysql_select_db('emailactivation') or die('could not connect to database ');
$qry = "select * from users";
$result = mysql_query($qry) or die(mysql_error());

$rows = mysql_num_rows($result);

for($i = 1;$i<=$rows;$i++)

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


echo $i . $ma['username'] . "
" ;

}
?>
--------------------------------------…
but m getting this output using the above script .. .

--------------------------------------…
1 name1
1 name2
1 name3
1 name4
1 name5
1 name6
1 name7
--------------------------------------…

whats wrong in my script .. i want to get the output which i have told at the start .. please help me whats wrong in my code to get the starting output ..



i have also tried this script ..
---------------------------------------…

Code: Select all

<?php
$conn = mysql_connect('localhost','root','') or die('could not connect to remote server');
mysql_select_db('emailactivation') or die('could not connect to database ');
$qry = "select * from users";
$result = mysql_query($qry) or die(mysql_error());

$rows = mysql_num_rows($result);
while($ma = mysql_fetch_array($result))
{
for($i = 1;$i<=$rows;$i++)

echo $i . $ma['username'] . "
" ;
}
?>
---------------------------------------…

and getting this output now ..

---------------------------------------…
1 name1
2 name1
3 name1
4 name1
5 name1

1 name2
2 name2
3 name2
4 name2
5 name2

---------------------------------------…

Re: Php mysql database question?

Posted: Tue Aug 16, 2011 12:54 pm
by Christopher
You only need one loop, not two nested loops:

Code: Select all

<?php
$conn = mysql_connect('localhost','root','') or die('could not connect to remote server');
mysql_select_db('emailactivation') or die('could not connect to database ');
$qry = "select * from users";
$result = mysql_query($qry) or die(mysql_error());

$i = 0;
while($ma = mysql_fetch_array($result)) {
     echo ++$i . ': ' . $ma['username'] . "\n" ;
}

Re: Php mysql database question?

Posted: Tue Aug 16, 2011 1:17 pm
by Pyrite
and then if you really want to get fancy ... :D

Code: Select all

SELECT @row := @row + 1 as counter, t.* FROM users t, (SELECT @row := 0) r
Then you can just do:

Code: Select all

while ($ma = mysql_fetch_array($result)) {
	echo $ma['counter'] . ':' . $ma['username'] . "\n";
}
:drunk: