How to output results from MySQL query that gives multiple results?

Hi,

The title was probably kind of vague. Basically, I have a query that gets all domains assigned to a username from a specific table:

[php]SELECT domain FROM domains WHERE username = ‘" . $_SESSION[“myusername”] . "’[/php]

So basically if you’re logged in and the username is stored in the session, it tries to match the username with any domains assigned to that name. If the table was:
[table]
[tr]
[td]Domain[/td][td]Username[/td]
[/tr]
[tr]
[td]testdomain1.com[/td][td]user143[/td]
[/tr]
[tr]
[td]testdomain2.com[/td][td]user143[/td]
[/tr]
[/table]

I would want to get both of those domains into separate variables with PHP. I just need to know how to accomplish this. Thank you!

This is really very simple. Here is the code for you:
[php]<?php
$mysqlink = mysql_connect(‘localhost’,‘mysql_user_name’,‘mysql_user_password’) or die(mysql_error());
mysql_select_db(‘mysql_db_name’,$mysqlink);

$r = mysql_query(“SELECT domain FROM domains WHERE username = '”.mysql_real_escape_string($_SESSION[“myusername”])."’");

$arr_domains = array();
if(mysql_num_rows($r)){
for($i=0;$i<mysql_num_rows($r);$i++){
$f = mysql_fetch_array($r);
$arr_domains[] = $f[‘domain’];
}

// echo all domains (comma delimited)
echo implode(', ',$arr_domains);

}
else{
// no domains for this user
}
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service