Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP-MySQL Select DISTINCT as PHP ARRAY. 1

Status
Not open for further replies.
Aug 12, 2002
11
Hello, I am trying to pull DISTINCT data from a MySQL DataBase and feed it into a php array.

Here is the query.
$DistinctServersFromDB = "SELECT DISTINCT Name FROM Server_Monitor ";
$result4 = mysql_query($DistinctServersFromDB) or die ("Query failed: " . mysql_error());
for ($i = mysql_num_rows($result4) - 1; $i >= 0; $i--) {
if (!mysql_data_seek($result4, $i)) {
echo "Cannot seek to row $i: " . mysql_error() . "\n"; continue ; }
if(!($row = mysql_fetch_object($result4))) continue ;
$q4Name = $row->Name ;
echo &quot; $q4Name <br> &quot; ; }


How can I get the data from this query into an array ?
If there is a more elegant way to do this please let me know.

Thanks for all your help... :)
dhaynes


 
mysql_fetch_array() will do the trick for you

Example code from the manual:
Code:
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        printf (&quot;ID: %s  Name: %s&quot;, $row[0], $row[1]);  
}
You can also get it to create an associative array:
Code:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        printf (&quot;ID: %s  Name: %s&quot;, $row[&quot;id&quot;], $row[&quot;name&quot;]);
    }

Both these will loop around for each row returned.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top