array keys

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

array keys

Post by bimo »

I haven't written any php in a while and the result is that now I'm beating myself up because I can't figure out why I can't pull the results out of a simple mysql db query. Does anyone see what I'm doing wrong?:

Code: Select all

$query = "SELECT DISTINCT
				artist
			FROM recordings 
			ORDER BY rand()
			LIMIT $num";       // when I run this query from querybrowser it works 
							
	$res = mysql_query($query);
	print("numrows=" . mysql_num_rows($res)); // prints the correct number
	print("<br/>");
	//print_r($res);  // this prints "Resource id#"
			
    // if results, print link
    if(isset($res)) // also tried if(mysql_num_rows($res) != 0)
	{
		echo "\n<table class=\"cloud\">";
		for($i = 0; $i < $h; $i++)
		{
			echo "\n\t<tr>";
			for($j = 0; $j < $w; $j++)
			{			
				$rows = mysql_num_rows($res);  //confirms that I am getting the correct # of rows
				
				echo "\n\t\t<td><a href=\"\">-> ".$res[$i][0]."</a></td>\n"; // Here is where the problem liies, I think.  $res[$i][0] doesn't print anything  I've also tried a few other things
			}
			print("\n\t</tr>");
		}
		echo "\n</table><br/><br>";
		    
    }
Thanks
Last edited by bimo on Sun Nov 19, 2006 1:13 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_fetch_array() may be of interest.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
User avatar
bimo
Forum Contributor
Posts: 100
Joined: Fri Apr 16, 2004 11:18 pm
Location: MD

Post by bimo »

Thanks, feyd. Now i have this:

Code: Select all

$res = mysql_query($query);
	print(mysql_num_rows($res));
	$res_names = mysql_fetch_assoc($res);
	foreach($res_names as $name){;
		print("<br/>");
		print($name);
	}
where $res is the result of a mysql_query()
and am getting this
20
Thomas, Oluyemi
I have tried print_r on $res_names, too, and it still only prints one result.

any ideas of what I'm doing wrong?

thanks
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

bimo wrote:any ideas of what I'm doing wrong?
You didn't read the example that php.net gave you :)

It gives you all the code you need...
Post Reply