Page 1 of 1

array keys

Posted: Sun Nov 19, 2006 10:31 am
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

Posted: Sun Nov 19, 2006 11:14 am
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.

Posted: Sun Nov 19, 2006 1:04 pm
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

Posted: Sun Nov 19, 2006 1:08 pm
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...