RE: [PHP] Why is this code not working?
[snip] I'm trying to put the results of a query into an array. Is there some simple error that I'm over looking? What's going on? [/snip] Have you tried mysql_fetch_array()? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Why is this code not working?
From: Dave G [EMAIL PROTECTED] PHP Gurus, If you say so... I'm trying to put the results of a query into an array. My code looks like this: ?php $query = SELECT datecolumn FROM table WHERE MONTH(datecolumn) = . $currentMonth; $result = mysql_query($query); $numRows = mysql_num_rows($result); for($i = 0; $i$numRows; $i++) { $workshops[$i] = mysql_fetch_row($result); echo $workshops[$i] . br /; } echo The results of the array are - . $workshops[0] . and . $workshops[1]; ? When I run this, the output to the screen says: --- Array Array The results of the array are - Array and Array --- Exactly what it should be. mysql_fetch_row returns an array, which you're assiging to $workshops[0] and $workshops[1]. Try: echo The results of the array are - . $workshops[0][0] . and . $workshops[1][0]; Or, even better... $query = SELECT datecolumn FROM table WHERE MONTH(datecolumn) = . $currentMonth; $result = mysql_query($query); while($r = mysql_fetch_row($result)) { $workshop[] = $r[0]; } echo The results of the array are - . $workshops[0] . and . $workshops[1]; ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Why is this code not working? [SOLVED]
PHP Gurus, If you say so... A few people have remarked about this. It's just a habit of mine. Whatever list I'm on, I address it to the gurus of that list. A guru being anyone who knows more than me, which is almost everyone. So on the CSS list, I address it to CSS Gurus. On the GoLive list, it's to GoLive Gurus. That's all. Try: echo The results of the array are - . $workshops[0][0] . and . $workshops[1][0]; I'm not sure if I did something wrong, but when I tried this, the results came back like this: The results of the array are - 2 and 2 I have no idea what the twos are referring to. while($r = mysql_fetch_row($result)) { $workshop[] = $r[0]; } This works perfectly! And, more importantly, I learned what it is that you are doing here. Taking the one column of the array returned from the mysql_fetch_row() command and putting it into a slightly simpler array. And for this purpose a while() loop is less code than a for() loop. Elegant. That's solved the problem. Thank you. Have you tried mysql_fetch_array()? Just thought I should mention this to Jay. Yes, I had been alternating my experiments with mysql_fetch_array() and mysql_fetch_row(), but the results, were exactly the same (or close enough that I don't remember otherwise). So I suspected that the problem lay elsewhere and didn't mention it. But thank you for suggesting alternatives. -- Cheers! Dave G [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php