Avatar of mimoser
mimoser
Flag for United States of America asked on

PHP MySQL display all entries in a column

Hello,

I'm trying to pull data out of a MySQL DB and show it on a php page. I would like to show all entries for a particular column, but every example I try either doesn't work, or this one shown below, that only shows 1. I'm thinking maybe I can loop or add another $row, but Im not sure. There could potantialy be a lot of entries, so im guessing it wiould need to loop until the end.

$result = mysql_query("SELECT name FROM s_users WHERE rank = 1 LIMIT 0 , 30") or die(mysql_error());  
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $sl = $row['name']; 

Open in new window


When that's echo'd it only shows 1 entry, when the db is showing several names that fit the criteria.. Is there also a way to show them in a row with comma seperators? name1, name2, name3...
PHPMySQL Server

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
maeltar

You need to itterate through the query results :

$result = mysql_query("SELECT name FROM s_users WHERE rank = 1 LIMIT 0 , 30") or die(mysql_error());  
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    // $sl = $row['name']; 
echo $row['name'] . "<br />";
}

Open in new window

mimoser

ASKER
Is there  a way to attach that to a variable for formatting reasons? Right now I have

$html2 .= '<tr><td colspan="8"><b>'.$sl.'</b></td></tr>';

Open in new window


Is there also a way to show them in a row with comma seperators? name1, name2, name3...
maeltar

Certainly...

$result = mysql_query("SELECT name FROM s_users WHERE rank = 1 LIMIT 0 , 30") or die(mysql_error());  
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){


$html2 .= '<tr><td colspan="8"><b> ' . $row['name'] . '</b></td></tr>';

}

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
mimoser

ASKER
Hello, I tried the suggestion but this way its still only pulling out one name at a time.
The example you provided earlier pulled out multiple names, but stacked them vertically, so I know they are in there, Im just not having any luck dragging them out. Below is the full function.

function userrank ()
{   
  $result = mysql_query("SELECT name FROM s_users WHERE rank = 1 LIMIT 0 , 30") or die(mysql_error());  
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){  
  $html2  = '';
	$html2 .= '<tr><td colspan="8"><b>:: Add Chat Staff</b></td></tr>';
	$html2 .= '<tr><td colspan="8"><b>' . $row['name'] . '</b></td></tr>'; 	
	$html2 .= '<tr align="center">';
	$html2 .= '<td>Staff Name</td>';
	$html2 .= '<td>Title</td>'; 
	$html2 .= '</tr>';
	$html2 .= '<tr align="center">';
	$html2 .= '<td><input type="text" name="userrank" value=""></td>';
	$html2 .= '<td>'.showSelectedID2('title','0').'</td>';
	$html2 .= '<td><input type="submit" name="addrank" value="Add"></td>';
	$html2 .= '<td>&nbsp;</td>';
	$html2 .= '</tr>'; 
	return $html2;
 }
}

Open in new window

ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

If you run that script segment and find only one name in the results set, you can be fairly sure that there is only one row in s_users that satisfied the WHERE clause.
mimoser

ASKER
I ran the coding snip and it gave me this result:

 string(11) "user1,user2"  

Open in new window


Which is how many rank = 1 there currently are.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mimoser

ASKER
Thank you Ray, this worked perfect for what I was trying to tie it into and its formatted great!!
Ray Paseur

Thanks for the points and thanks for using EE.  All the best, ~Ray