Skip to main content
Inspiring
October 25, 2006
Question

OT: PHP looping through an array

  • October 25, 2006
  • 30 replies
  • 1620 views
When I build a recordset, I create an array, right?

So, if I have this -

mysql_select_db($database_touchstone, $touchstone);
$query_rsCategory = "SELECT DISTINCT tclientsCategory FROM
tbl_orderedclients";
$rsCategory = mysql_query($query_rsCategory, $touchstone) or
die(mysql_error());
$row_rsCategory = mysql_fetch_assoc($rsCategory);
$totalRows_rsCategory = mysql_num_rows($rsCategory);

The variable called $row_rsCategory contains an array of all values in the
recordset, right?

So, why can't I get those values like this?

foreach ($row_rsCategory as $value) {
echo $value."<br>";
}


--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================



This topic has been closed for replies.

30 replies

Inspiring
October 27, 2006
Yeah - and the halcyon days behind the grammar school.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Steve" <me@here.com> wrote in message
news:c5f4k2939vvqskqtbtn36pqsa4a20q8s5n@4ax.com...
> On Fri, 27 Oct 2006 11:03:25 +0100, "gareth" <gareth@buzzinet.co.uk>
> wrote:
>
>>Yes it is, although I`ve expanded on it over time for my own use.
>
> Hand it over or I spill the beans about your bartender days! ;-)
> --
> Steve
> steve at flyingtigerwebdesign dot com


Participating Frequently
October 27, 2006
I've allways noticed how alot of people use the builtin database functions, and as someone who has allways used a simple database class, it seems clunky. :)
Inspiring
October 27, 2006
On Fri, 27 Oct 2006 11:03:25 +0100, "gareth" <gareth@buzzinet.co.uk>
wrote:

>Yes it is, although I`ve expanded on it over time for my own use.

Hand it over or I spill the beans about your bartender days! ;-)
--
Steve
steve at flyingtigerwebdesign dot com
Inspiring
October 27, 2006
Can I ask how ?

--

Dave Buchholz
I-CRE8
www.i-cre8.co.uk
Skype ID: I-CRE8


"gareth" <gareth@buzzinet.co.uk> wrote in message
news:ehslha$f68$1@forums.macromedia.com...
Yes it is, although I`ve expanded on it over time for my own use.

Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.



Inspiring
October 27, 2006
Yes it is, although I`ve expanded on it over time for my own use.

Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.


Inspiring
October 26, 2006
Murray,

http://www.dmxzone.com/ShowDetail.asp?NewsId=5106 is the article I was
referring to on DMX Zone

--

Dave Buchholz
I-CRE8
www.i-cre8.co.uk
Skype ID: I-CRE8


"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:ehq9r8$h6l$1@forums.macromedia.com...
And it can be found there, too?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Dave Buchholz" <forums@REMOVETHISi-cre8.co.uk> wrote in message
news:ehq8tj$g3e$1@forums.macromedia.com...
> Gareth,
>
> Is this the class you wrote about for DMX Zone ?
>
> --
>
> Dave Buchholz
> I-CRE8
> www.i-cre8.co.uk
> Skype ID: I-CRE8
>
>
> "gareth" <gareth@buzzinet.co.uk> wrote in message
> news:ehq0kq$700$1@forums.macromedia.com...
> Its completely doable, I created a custom database class years back, which
> does this. I use it in every project and it makes it so much easier to
> work
> with the database, and has saved me untold amount of time. To read a
> recordset you just use
>
> $db = new Database();
> $sql = "SELECT * FROM table";
> $db->dbOpenDatabase();
> $results = $db->dbResults($sql);
> $db->dbCloseDatabase();
>
> $results is then an array of record arrays, and you can loop through it
> easily with foreach()
>
> The dbResults function is:
>
> function dbResults($query){
> // Execute an SQL query and return results in an Array
> $dbResult = mysql_query($query, $this->dbLink)
> or die ("Database: MySQL Error: " . mysql_error() );
> $numRecords = mysql_num_rows($dbResult);
> $dataArray = "";
> for($i=0;$i<$numRecords;$i++){
> $row = mysql_fetch_assoc($dbResult);
> $dataArray[] = $row;
> }
> return $dataArray;
> }
>
> Obviously you`d need to change the parameters to put in the link to the
> database etc
>
> Its well worth making a custom database class, as it can become invaluable
> and saves coding the same things over and over. I have the basic functions
> in one class:
>
> dbOpenDatabase()
> dbResults()
> dbNumRecords()
> dbAddRecord()
> dbDeleteRecord()
> dbUpdateRecord()
> dbCloseDatabase()
>
> dbAddRecord,dbDeleteRecord,dbUpdateRecord all do the same thing, ie
> running
> the passed sql but it makes the code easier to read.
>
> Any extra functions you need for projects can be put in another class that
> extends the basic database class.
>
> Gareth
> http://www.phploginsuite.co.uk/
> PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.
>
>
>



Inspiring
October 26, 2006
And it can be found there, too?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Dave Buchholz" <forums@REMOVETHISi-cre8.co.uk> wrote in message
news:ehq8tj$g3e$1@forums.macromedia.com...
> Gareth,
>
> Is this the class you wrote about for DMX Zone ?
>
> --
>
> Dave Buchholz
> I-CRE8
> www.i-cre8.co.uk
> Skype ID: I-CRE8
>
>
> "gareth" <gareth@buzzinet.co.uk> wrote in message
> news:ehq0kq$700$1@forums.macromedia.com...
> Its completely doable, I created a custom database class years back, which
> does this. I use it in every project and it makes it so much easier to
> work
> with the database, and has saved me untold amount of time. To read a
> recordset you just use
>
> $db = new Database();
> $sql = "SELECT * FROM table";
> $db->dbOpenDatabase();
> $results = $db->dbResults($sql);
> $db->dbCloseDatabase();
>
> $results is then an array of record arrays, and you can loop through it
> easily with foreach()
>
> The dbResults function is:
>
> function dbResults($query){
> // Execute an SQL query and return results in an Array
> $dbResult = mysql_query($query, $this->dbLink)
> or die ("Database: MySQL Error: " . mysql_error() );
> $numRecords = mysql_num_rows($dbResult);
> $dataArray = "";
> for($i=0;$i<$numRecords;$i++){
> $row = mysql_fetch_assoc($dbResult);
> $dataArray[] = $row;
> }
> return $dataArray;
> }
>
> Obviously you`d need to change the parameters to put in the link to the
> database etc
>
> Its well worth making a custom database class, as it can become invaluable
> and saves coding the same things over and over. I have the basic functions
> in one class:
>
> dbOpenDatabase()
> dbResults()
> dbNumRecords()
> dbAddRecord()
> dbDeleteRecord()
> dbUpdateRecord()
> dbCloseDatabase()
>
> dbAddRecord,dbDeleteRecord,dbUpdateRecord all do the same thing, ie
> running
> the passed sql but it makes the code easier to read.
>
> Any extra functions you need for projects can be put in another class that
> extends the basic database class.
>
> Gareth
> http://www.phploginsuite.co.uk/
> PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.
>
>
>


Inspiring
October 26, 2006
Gareth,

Is this the class you wrote about for DMX Zone ?

--

Dave Buchholz
I-CRE8
www.i-cre8.co.uk
Skype ID: I-CRE8


"gareth" <gareth@buzzinet.co.uk> wrote in message
news:ehq0kq$700$1@forums.macromedia.com...
Its completely doable, I created a custom database class years back, which
does this. I use it in every project and it makes it so much easier to work
with the database, and has saved me untold amount of time. To read a
recordset you just use

$db = new Database();
$sql = "SELECT * FROM table";
$db->dbOpenDatabase();
$results = $db->dbResults($sql);
$db->dbCloseDatabase();

$results is then an array of record arrays, and you can loop through it
easily with foreach()

The dbResults function is:

function dbResults($query){
// Execute an SQL query and return results in an Array
$dbResult = mysql_query($query, $this->dbLink)
or die ("Database: MySQL Error: " . mysql_error() );
$numRecords = mysql_num_rows($dbResult);
$dataArray = "";
for($i=0;$i<$numRecords;$i++){
$row = mysql_fetch_assoc($dbResult);
$dataArray[] = $row;
}
return $dataArray;
}

Obviously you`d need to change the parameters to put in the link to the
database etc

Its well worth making a custom database class, as it can become invaluable
and saves coding the same things over and over. I have the basic functions
in one class:

dbOpenDatabase()
dbResults()
dbNumRecords()
dbAddRecord()
dbDeleteRecord()
dbUpdateRecord()
dbCloseDatabase()

dbAddRecord,dbDeleteRecord,dbUpdateRecord all do the same thing, ie running
the passed sql but it makes the code easier to read.

Any extra functions you need for projects can be put in another class that
extends the basic database class.

Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.



Inspiring
October 26, 2006
Its completely doable, I created a custom database class years back, which
does this. I use it in every project and it makes it so much easier to work
with the database, and has saved me untold amount of time. To read a
recordset you just use

$db = new Database();
$sql = "SELECT * FROM table";
$db->dbOpenDatabase();
$results = $db->dbResults($sql);
$db->dbCloseDatabase();

$results is then an array of record arrays, and you can loop through it
easily with foreach()

The dbResults function is:

function dbResults($query){
// Execute an SQL query and return results in an Array
$dbResult = mysql_query($query, $this->dbLink)
or die ("Database: MySQL Error: " . mysql_error() );
$numRecords = mysql_num_rows($dbResult);
$dataArray = "";
for($i=0;$i<$numRecords;$i++){
$row = mysql_fetch_assoc($dbResult);
$dataArray[] = $row;
}
return $dataArray;
}

Obviously you`d need to change the parameters to put in the link to the
database etc

Its well worth making a custom database class, as it can become invaluable
and saves coding the same things over and over. I have the basic functions
in one class:

dbOpenDatabase()
dbResults()
dbNumRecords()
dbAddRecord()
dbDeleteRecord()
dbUpdateRecord()
dbCloseDatabase()

dbAddRecord,dbDeleteRecord,dbUpdateRecord all do the same thing, ie running
the passed sql but it makes the code easier to read.

Any extra functions you need for projects can be put in another class that
extends the basic database class.

Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.


Inspiring
October 25, 2006
yeah, nevermind, iv'e got too many irons in the far.

jon

"Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
news:ehog5g$bpr$1@forums.macromedia.com...
> Who, me?
>
> --
> Murray --- ICQ 71997575
> Adobe Community Expert
> (If you *MUST* email me, don't LAUGH when you do so!)
> ==================
> http://www.dreamweavermx-templates.com - Template Triage!
> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
> http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
> ==================
>
>
> "crash" <crash@bcdcdigital.com> wrote in message
> news:ehofb5$amv$1@forums.macromedia.com...
>> but you can use seek to get the next one, yes? is that what you were
>> thinking of?
>>
>> "Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
>> news:ehof3c$aeb$1@forums.macromedia.com...
>>> Neither am I.
>>>
>>> I now understand that the fetch array function only gets a single row
>>> from the table into that array.
>>>
>>> --
>>> Murray --- ICQ 71997575
>>> Adobe Community Expert
>>> (If you *MUST* email me, don't LAUGH when you do so!)
>>> ==================
>>> http://www.dreamweavermx-templates.com - Template Triage!
>>> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
>>> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
>>> http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
>>> ==================
>>>
>>>
>>> "Michael Fesser" <netizen@gmx.de> wrote in message
>>> news:23evj29ejpg9d6hdco00pm756octphqsnu@4ax.com...
>>>> .oO(Murray *ACE*)
>>>>
>>>>>Right - but I meant replace the business end of the while statement
>>>>>with the
>>>>>fetch_array() function?
>>>>
>>>> Hmm? Not sure what you mean.
>>>>
>>>> Micha
>>>
>>>
>>
>>
>
>


Inspiring
October 25, 2006
Who, me?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"crash" <crash@bcdcdigital.com> wrote in message
news:ehofb5$amv$1@forums.macromedia.com...
> but you can use seek to get the next one, yes? is that what you were
> thinking of?
>
> "Murray *ACE*" <forums@HAHAgreat-web-sights.com> wrote in message
> news:ehof3c$aeb$1@forums.macromedia.com...
>> Neither am I.
>>
>> I now understand that the fetch array function only gets a single row
>> from the table into that array.
>>
>> --
>> Murray --- ICQ 71997575
>> Adobe Community Expert
>> (If you *MUST* email me, don't LAUGH when you do so!)
>> ==================
>> http://www.dreamweavermx-templates.com - Template Triage!
>> http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
>> http://www.dwfaq.com - DW FAQs, Tutorials & Resources
>> http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
>> ==================
>>
>>
>> "Michael Fesser" <netizen@gmx.de> wrote in message
>> news:23evj29ejpg9d6hdco00pm756octphqsnu@4ax.com...
>>> .oO(Murray *ACE*)
>>>
>>>>Right - but I meant replace the business end of the while statement with
>>>>the
>>>>fetch_array() function?
>>>
>>> Hmm? Not sure what you mean.
>>>
>>> Micha
>>
>>
>
>