[PHP] Why is this code not working?

2003-11-12 Thread Dave G
PHP Gurus,
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
---
If I change just this line (take the square brackets off the
$workshops array declaration):
$workshops = mysql_fetch_row($result);

Then my results are:
---
2003-11-17

The results of the Array are 2003-11-24 and
---
I understand that by not using square brackets to declare the
variable explicitly as an array, then it just stores the last value,
which is why the dates aren't echoing to the screen where they are
supposed to. That part makes sense. What I don't understand is why when
I try to put the results into a particular location in the array, it
gets stored as the word Array. But if I just declare it as a regular
variable, then I get the date that I want.
I've been writing this out a million different ways to try and
figure out what's going on, I've searched Google, and I've read the
descriptions for the mysql_* commands on php.net. But everything I read
indicates it should work. In fact I got the for() loop from a tutorial
web site, so I'm pretty sure it's supposed to work. I can't see what's
wrong. Am I not declaring the array correctly? Is there some simple
error that I'm over looking? What's going on?

-- 
Cheers!
Dave G
[EMAIL PROTECTED]

-- 
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?

2003-11-12 Thread Jay Blanchard
[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?

2003-11-12 Thread CPT John W. Holmes
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]

2003-11-12 Thread Dave G
  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