Solved

PHP MySQL display all entries in a column

Posted on 2012-03-12
9
347 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 110

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 110

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 110

Expert Comment

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 dynamically set the form action using jQuery.

696 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