[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

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...
0
mimoser
Asked:
mimoser
  • 4
  • 3
  • 2
1 Solution
 
maeltarCommented:
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

0
 
mimoserAuthor Commented:
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...
0
 
maeltarCommented:
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mimoserAuthor Commented:
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

0
 
Ray PaseurCommented:
It might be easier to eat this one byte at a time... This will show you how to get the names into a comma-separated list.
$csv = NULL;
$result = mysql_query("SELECT name FROM s_users WHERE rank = 1 LIMIT 0 , 30") or die(mysql_error());  
while ($row = mysql_fetch_assoc($result))
{
    $arr{} = $row["name"];
}
$csv = implode(',', $arr);
var_dump($csv);

Open in new window

0
 
Ray PaseurCommented:
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.
0
 
mimoserAuthor Commented:
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.
0
 
mimoserAuthor Commented:
Thank you Ray, this worked perfect for what I was trying to tie it into and its formatted great!!
0
 
Ray PaseurCommented:
Thanks for the points and thanks for using EE.  All the best, ~Ray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now