thecoolone wrote:
> Jerry Stuckle wrote:
>
>>thecoolone wrote:
>>
>>>Jerry Stuckle wrote:
>>>
>>>
>>>>No problem using mysql_fetch_array - but that fetches ONE ROW into an
>>>>array. If you want to get all of the rows, you need to continue issuing
>>>>mysql_fetch_array calls until it returns false.
>>>
>>>
>>>i tried that as follows:
>>>while ($count = mysql_fetch_array($result,MYSQL_***OC))
>>>{
>>>print_r($count);
>>>print "<br>"; }
>>>
>>>but i want to be able to store the all results of mysql_fetch_array in
>>>one single array.
>>>i thought of using a for loop like:
>>>$number_of_rows = mysql_num_rows($result);
>>>for ($i=0;$i<$number_of_rows;$i++)
>>>{
>>>print_r($count[$i]);
>>>print "<br>";
>>>global $num;
>>>$num=array();
>>>$num=$count;
>>>}
>>>
>>>but it isint working the way i expected.
>>>
>>
>>No, because you never put anything in $count, so printing it won't show
>>anything. You need to fetch the data from the mysql result before you
>>can us it.
>>
>>Also every time through your loop you're reinitializing $num.
>
>
> Then how do i loop in such a way that it appends the value of
> mysql_fetch_array to $count
>
See the examples at
http://www.php.net/manual/en/functio...etch-***oc.php and
http://www.php.net/manual/en/functio...etch-array.php. They have
some good examples.
Your first try has the right idea - but you overwrite $count each time
when you fetch the new row. Additionally, the array in $count only
represents a single row. If you want multiple elements, you need an
array of arrays, i.e.
$count = array();
while ($temp = mysql_fetch_array($result), MYSQL_***SOC) {
$count [] = $temp;
....
This creates the $count array then appends each row read into it.
>
>>>>But if all you want is a count of id's in an array, let MySQL do it for
>>>>you. Check out the COUNT SQL function, GROUP BY clause and JOIN clauses.
>>>>
>>>>I don't know your table structure or data, but something like:
>>>>
>>>> SELECT id, COUNT(id) FROM table1
>>>> JOIN table2 ON table1.id = table2.id
>>>> GROUP BY id
>
>
>>>the problem here is that some rows have id's separated by commas.
>>>and so doing a count(id) wont work here.
>>>
>>
>>That's a broken database design then. Do a search on "database
>>normalization". You should probably have a third table which links the
>>two current tables together.
>
>
> its not a broken db design. There is a reason for that, which i had
> wrote in a mail i had sent you.
> the reason being each id's indicate the files that the user has choose
> to download.
> i want to be able to scan through each row of $count array and if the
> id is present in that row
> then increment the count for that id a $counter array (which is
> ***ociative). If my thinking is correct
> i will have to do an in_array comparision for each id in the first
> table against the $count array and then
> increment $counter right??
>
> thank you.
>
To start, it is a violation of first normal form (having more than one
value in a column). I have *never* found a reason this is "better" than
the correct way of doing things - with a link table. And I've been
doing RDB design for over 20 years, starting with DB2 on mainframes.
And yes, when I say never, I do mean *never*.
Read up on database normalization. You will find a correct database
design will make this job a lot easier (plus give you more options). In
fact, you'll be able to get your count entirely in SQL without the need
for PHP or any other language.
Also, as I said before - IMHO newsgroup threads should remain in the
newsgroup. You can explain why you have such a design here. I tend to
delete email related to newsgroup messages.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
EMAIL REMOVED
==================