Php mysql database question?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mittul
Forum Newbie
Posts: 4
Joined: Tue Jul 26, 2011 4:27 am

Php mysql database question?

Post 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

---------------------------------------…
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Php mysql database question?

Post 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" ;
}
(#10850)
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Re: Php mysql database question?

Post 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:
Post Reply