[PHP-DB] foreach loop from MySQL select query to HTML select list
Hi, I'm trying to pick up data from my MySQL database, use a foreach loop to extact the data from the result & put that data into a select list. I have tried the following code with & without the $row = ... line, both unsucessfully: -select- $value\n";} ?> I get the following error message in my HTML output code: Warning: 1 is not a valid MySQL-Link resource in /home/.../public_html/content.php on line 48 Warning: Invalid argument supplied for foreach() in /home/.../public_html/content.php on line 49 Line 48 is the line begining with: foreach... Any help would be appreciated. Thanks, Gav This e-mail and any attachments are intended solely for the named addressee, are confidential and may contain legally privileged information. The copying or distribution of them or of any information they contain, by anyone other than the addressee, is prohibited. If you received this e-mail in error, please notify us immediately by return e-mail or telephone +61 2 9413 2944 and destroy the original message. Thank you. As Email is subject to viruses we advise that all Emails and any attachments should be scanned by an up to-date Anti Virus programme automatically by your system. It is the responsibility of the recipient to ensure that all Emails and any attachments are cleared of Viruses before opening. KSG can not accept any responsibility for viruses that maybe contained here in. Please advise KSG by return Email if you believe any Email sent by our system may contain a virus. It should be noted that most Anti Virus programmes can not scan encrypted file attachments (example - documents saved with a password). Thus extra care should be taken when opening these files. Liability limited by the Accountants Scheme, approved under the Professional Standards Act 1994 (NSW). Level 4 54 Neridah StreetPO Box 1290 CHATSWOOD NSW 2067 CHATSWOOD NSW 2057 Ph: +61 2 9413 2944 Fax: +61 2 9413 9901 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
good point! whoops. ok, i've moved the $result_dept & $result_deptsub queries, now i'm getting 2 of the same entries in my html code: -select- audit audit any thoughts? Gav -Original Message- From: John Coder [mailto:[EMAIL PROTECTED]] Sent: Thursday, 28 November 2002 3:44 PM To: Gavin Amm Cc: Php-Db (E-mail) Subject: Re: [PHP-DB] foreach loop from MySQL select query to HTML select list On Wed, 2002-11-27 at 23:33, Gavin Amm wrote: > Hi, > > I'm trying to pick up data from my MySQL database, use a foreach loop to > extact the data from the result & put that data into a select list. > > I have tried the following code with & without the $row = ... line, both > unsucessfully: > > $db = mysql_connect("myhost", "username", ""); > mysql_select_db("mydatabase", $db); > > ## FETCH INFO > $sql_dept = "SELECT dept FROM content GROUP BY dept"; > $sql_deptsub = "SELECT deptsub FROM content GROUP BY deptsub"; > > mysql_close($db); How can run a query when it's closed? move this to after the select and see if it works. > ?> > > > > -select- >$result = mysql_query($sql_dept, $db); > $row = mysql_fetch_array($result); > foreach ($row as $value) {echo " value=\"$value\">$value\n";} > ?> > Put your mysql_close here if you want to close it. John Coder -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php This e-mail and any attachments are intended solely for the named addressee, are confidential and may contain legally privileged information. The copying or distribution of them or of any information they contain, by anyone other than the addressee, is prohibited. If you received this e-mail in error, please notify us immediately by return e-mail or telephone +61 2 9413 2944 and destroy the original message. Thank you. As Email is subject to viruses we advise that all Emails and any attachments should be scanned by an up to-date Anti Virus programme automatically by your system. It is the responsibility of the recipient to ensure that all Emails and any attachments are cleared of Viruses before opening. KSG can not accept any responsibility for viruses that maybe contained here in. Please advise KSG by return Email if you believe any Email sent by our system may contain a virus. It should be noted that most Anti Virus programmes can not scan encrypted file attachments (example - documents saved with a password). Thus extra care should be taken when opening these files. Liability limited by the Accountants Scheme, approved under the Professional Standards Act 1994 (NSW). Level 4 54 Neridah StreetPO Box 1290 CHATSWOOD NSW 2067 CHATSWOOD NSW 2057 Ph: +61 2 9413 2944 Fax: +61 2 9413 9901 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
ok, i've looked around the web a bit more & it would appear you can't just use a foreach function, apparently you need to use a while{foreach{}} structure. i was hoping you could just use the foreach function? so this is the code now: -select- $value\n"; } } ?> but now the output HTML code is doubling up: -select- audit audit consulting consulting ... cheers, Gav This e-mail and any attachments are intended solely for the named addressee, are confidential and may contain legally privileged information. The copying or distribution of them or of any information they contain, by anyone other than the addressee, is prohibited. If you received this e-mail in error, please notify us immediately by return e-mail or telephone +61 2 9413 2944 and destroy the original message. Thank you. As Email is subject to viruses we advise that all Emails and any attachments should be scanned by an up to-date Anti Virus programme automatically by your system. It is the responsibility of the recipient to ensure that all Emails and any attachments are cleared of Viruses before opening. KSG can not accept any responsibility for viruses that maybe contained here in. Please advise KSG by return Email if you believe any Email sent by our system may contain a virus. It should be noted that most Anti Virus programmes can not scan encrypted file attachments (example - documents saved with a password). Thus extra care should be taken when opening these files. Liability limited by the Accountants Scheme, approved under the Professional Standards Act 1994 (NSW). Level 4 54 Neridah StreetPO Box 1290 CHATSWOOD NSW 2067 CHATSWOOD NSW 2057 Ph: +61 2 9413 2944 Fax: +61 2 9413 9901 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] foreach loop from MySQL select query to HTML select list
On Thursday 28 November 2002 14:21, Gavin Amm wrote: > ok, > > i've looked around the web a bit more & it would appear you can't just use > a foreach function, apparently you need to use a while{foreach{}} > structure. i was hoping you could just use the foreach function? > > so this is the code now: > > > -select- > while ($row = mysql_fetch_array($result_deptsub)){ > foreach ($row as $value) { > echo "$value\n"; > } > } > ?> > > > > but now the output HTML code is doubling up: > > > -select- > audit > audit > consulting > consulting > ... > mysql_fetch_array() by default returns an array that's associative AND numeric (print_r($row) to see what it looks like). Use mysql_fetch_assoc() instead. -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* "There are things that are so serious that you can only joke about them" - Heisenberg */ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
> i've looked around the web a bit more & it would appear you can't just use > a > foreach function, apparently you need to use a while{foreach{}} structure. > i > was hoping you could just use the foreach function? > > so this is the code now: > > > -select- > while ($row = mysql_fetch_array($result_deptsub)){ > foreach ($row as $value) { > echo "$value\n"; Where did you get the idea to use foreach? Your select statement is only selecting one column, so $row is only going to have one column in it for each mysql_fetch_*. Use something like this: while($row = mysql_fetch_array($result_deptsub)) { echo "{$row['deptsub']}\n"; } ---John Holmes... -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
hmm, interesting to know the mysql_fetch_array() by default returns an array that's associative AND numeric - very useful to know & something i'll have to look into more, thanks Jason. Thanks also John, puting curley braces around the variables is a valuable lesson - works wonderfully - cheers. i do have one more problem though, i need to run the loop a 2nd time later in the page in exactly the same way, but nothing is returned. i just copied & pasted the code from one section of the page to the next. do i need to reset one of the vaiables? -select- {$row['dept']}\n"; } ?> cheers, Gav -Original Message- From: John W. Holmes [mailto:[EMAIL PROTECTED]] Sent: Friday, 29 November 2002 2:59 AM To: 'Gavin Amm'; 'Php-Db (E-mail)' Subject: RE: [PHP-DB] foreach loop from MySQL select query to HTML select list > i've looked around the web a bit more & it would appear you can't just use > a > foreach function, apparently you need to use a while{foreach{}} structure. > i > was hoping you could just use the foreach function? > > so this is the code now: > > > -select- > while ($row = mysql_fetch_array($result_deptsub)){ > foreach ($row as $value) { > echo "$value\n"; Where did you get the idea to use foreach? Your select statement is only selecting one column, so $row is only going to have one column in it for each mysql_fetch_*. Use something like this: while($row = mysql_fetch_array($result_deptsub)) { echo "{$row['deptsub']}\n"; } ---John Holmes... This e-mail and any attachments are intended solely for the named addressee, are confidential and may contain legally privileged information. The copying or distribution of them or of any information they contain, by anyone other than the addressee, is prohibited. If you received this e-mail in error, please notify us immediately by return e-mail or telephone +61 2 9413 2944 and destroy the original message. Thank you. As Email is subject to viruses we advise that all Emails and any attachments should be scanned by an up to-date Anti Virus programme automatically by your system. It is the responsibility of the recipient to ensure that all Emails and any attachments are cleared of Viruses before opening. KSG can not accept any responsibility for viruses that maybe contained here in. Please advise KSG by return Email if you believe any Email sent by our system may contain a virus. It should be noted that most Anti Virus programmes can not scan encrypted file attachments (example - documents saved with a password). Thus extra care should be taken when opening these files. Liability limited by the Accountants Scheme, approved under the Professional Standards Act 1994 (NSW). Level 4 54 Neridah StreetPO Box 1290 CHATSWOOD NSW 2067 CHATSWOOD NSW 2057 Ph: +61 2 9413 2944 Fax: +61 2 9413 9901 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
> hmm, interesting to know the mysql_fetch_array() by default returns an > array > that's associative AND numeric - very useful to know & something i'll have > to look into more, thanks Jason. > > Thanks also John, puting curley braces around the variables is a valuable > lesson - works wonderfully - cheers. > > i do have one more problem though, i need to run the loop a 2nd time later > in the page in exactly the same way, but nothing is returned. > i just copied & pasted the code from one section of the page to the next. > do i need to reset one of the vaiables? > > > > -select- > while($row = mysql_fetch_array($result_dept)){ > echo "{$row['dept']}\n"; > } > ?> > Put the options into a variable and just echo it out where you need it. $options = "-select-\n"; while($row = mysql_fetch_row($result)) { $options .= "{$row[0]}\n"; } And wherever you need a select statement, just ---John Holmes... -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
thanks John, i was thinking along similar lines. that should work well. cheers, Gav -Original Message- From: John W. Holmes [mailto:[EMAIL PROTECTED]] Sent: Friday, 29 November 2002 4:08 PM To: 'Gavin Amm'; 'Php-Db (E-mail)' Subject: RE: [PHP-DB] foreach loop from MySQL select query to HTML select list > hmm, interesting to know the mysql_fetch_array() by default returns an > array > that's associative AND numeric - very useful to know & something i'll have > to look into more, thanks Jason. > > Thanks also John, puting curley braces around the variables is a valuable > lesson - works wonderfully - cheers. > > i do have one more problem though, i need to run the loop a 2nd time later > in the page in exactly the same way, but nothing is returned. > i just copied & pasted the code from one section of the page to the next. > do i need to reset one of the vaiables? > > > > -select- > while($row = mysql_fetch_array($result_dept)){ > echo "{$row['dept']}\n"; > } > ?> > Put the options into a variable and just echo it out where you need it. $options = "-select-\n"; while($row = mysql_fetch_row($result)) { $options .= "{$row[0]}\n"; } And wherever you need a select statement, just ---John Holmes... -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php This e-mail and any attachments are intended solely for the named addressee, are confidential and may contain legally privileged information. The copying or distribution of them or of any information they contain, by anyone other than the addressee, is prohibited. If you received this e-mail in error, please notify us immediately by return e-mail or telephone +61 2 9413 2944 and destroy the original message. Thank you. As Email is subject to viruses we advise that all Emails and any attachments should be scanned by an up to-date Anti Virus programme automatically by your system. It is the responsibility of the recipient to ensure that all Emails and any attachments are cleared of Viruses before opening. KSG can not accept any responsibility for viruses that maybe contained here in. Please advise KSG by return Email if you believe any Email sent by our system may contain a virus. It should be noted that most Anti Virus programmes can not scan encrypted file attachments (example - documents saved with a password). Thus extra care should be taken when opening these files. Liability limited by the Accountants Scheme, approved under the Professional Standards Act 1994 (NSW). Level 4 54 Neridah StreetPO Box 1290 CHATSWOOD NSW 2067 CHATSWOOD NSW 2057 Ph: +61 2 9413 2944 Fax: +61 2 9413 9901 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] foreach loop from MySQL select query to HTML select list
At 10:40 AM 11/29/02 +1100, Gavin Amm wrote: hmm, interesting to know the mysql_fetch_array() by default returns an array that's associative AND numeric - very useful to know & something i'll have to look into more, thanks Jason. Using PHP's built in template system... $result_dept = mysql_query( "SELECT DepartmentID, Department FROM Departments ORDER BY Department" ); ... -select- i do have one more problem though, i need to run the loop a 2nd time later in the page in exactly the same way, but nothing is returned. i just copied & pasted the code from one section of the page to the next. do i need to reset one of the vaiables? mysql_data_seek() http://www.php.net/manual/en/function.mysql-data-seek.php Rick -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php