Solved

PHP MySQL display all entries in a column

Posted on 2012-03-12
9
341 Views
Last Modified: 2012-03-12
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
Comment
Question by:mimoser
  • 4
  • 3
  • 2
9 Comments
 
LVL 11

Expert Comment

by:maeltar
Comment Utility
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
 

Author Comment

by:mimoser
Comment Utility
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
 
LVL 11

Expert Comment

by:maeltar
Comment Utility
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
 

Author Comment

by:mimoser
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:mimoser
Comment Utility
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
 

Author Closing Comment

by:mimoser
Comment Utility
Thank you Ray, this worked perfect for what I was trying to tie it into and its formatted great!!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE.  All the best, ~Ray
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now