Solved

PHP MySQL display all entries in a column

Posted on 2012-03-12
9
345 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
ID: 37710732
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
ID: 37710813
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
ID: 37711314
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:mimoser
ID: 37711914
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37712167
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 109

Expert Comment

by:Ray Paseur
ID: 37712170
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
ID: 37712248
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
ID: 37712275
Thank you Ray, this worked perfect for what I was trying to tie it into and its formatted great!!
0
 
LVL 109

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

805 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