By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,748 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,748 IT Pros & Developers. It's quick & easy.

foreach

P: n/a
Hi
What's the problem here:

odbc_fetch_into($rs, $row, &$arr);
foreach ($arr as $elem) {
echo $elem;
echo " ";
}

I get the error:

Warning: Invalid argument supplied for foreach() in
C:\xampp\htdocs\Abschlussprojekt\auswertung.php on line 15

altough I copied this from a book.
Thanks and regards
Michael
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 12 Jul 2004 16:03:53 -0700, sg***@dplanet.ch (Michael) wrote:
What's the problem here:

odbc_fetch_into($rs, $row, &$arr);
foreach ($arr as $elem) {
echo $elem;
echo " ";
}

I get the error:

Warning: Invalid argument supplied for foreach() in
C:\xampp\htdocs\Abschlussprojekt\auswertung.php on line 15
$arr isn't an array.
altough I copied this from a book.


Does the book mention error checking and what happens when you run out of
rows?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
Hi
Nope...it doesn't seem to be very usable though. Well but why this is not
working? Could you post the correct syntax please.
Thanks and regards from Switzerland
Michael

$sql = "SELECT Name FROM kunden";
$rs=odbc_exec($con, $sql); #the connection is ok
$row=1;
odbc_fetch_into($rs, $row, &$arr);
foreach ($row as $arr) {
echo $arr;
echo " "; #I get only one Name
}
Jul 17 '05 #3

P: n/a
*** Michael wrote/escribió (13 Jul 2004 01:36:01 -0700):
odbc_fetch_into($rs, $row, &$arr);
foreach ($row as $arr) {
echo $arr;
echo " "; #I get only one Name
}


foreach($arr as $row){
}

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #4

P: n/a
No this doesn't work neither. I believe now this to be a bug. I tried
20 examples from php.net and now I'm angry.
my question now is how to show all records this way:

odbc_fetch_row($rs, $row);
$num=odbc_num_rows($rs);
for ($col=1;$col<$num+1;$col++) {
echo odbc_result($rs, $col);
echo " ";
}

Acutally I only get one row....Thanks indeed.
Regards Michael
Jul 17 '05 #5

P: n/a
I noticed that Message-ID:
<ca**************************@posting.google.com > from Michael contained
the following:

odbc_fetch_row($rs, $row);
$num=odbc_num_rows($rs);
for ($col=1;$col<$num+1;$col++) {
echo odbc_result($rs, $col);
echo " ";
}

Acutally I only get one row....Thanks indeed.
Regards Michael


That's because odbc_num_rows may not work properly with Select
statements. Crazy, huh?
See http://uk2.php.net/manual/en/function.odbc-num-rows.php

Here's what I use

$odbc=odbc_connect ('data_source_name', '', '') or die( "Could Not
Connect to ODBC Database!" );

$sql="SELECT * from table";
$result=odbc_exec($odbc,$sql);
while($myrow=odbc_fetch_array($result)){
print "Field1 = ".$myrow['field1']."<br>";
print "Field2 = ".$myrow['field2']."<br>";
print "Fieldn = ".$myrow['fieldn']."<br>";
}

Hope this helps. If you need the number of rows just stick a counter in
the loop.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

P: n/a
*** Michael wrote/escribió (13 Jul 2004 13:35:55 -0700):
No this doesn't work neither. I believe now this to be a bug.


Use this and find out what your variables contain. It'll be faster than
merely copying book examples and making them fit in your code:

echo gettype($myvariable);
echo '<pre>'; print_r($myvariable); echo '</pre>';

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #7

P: n/a
Michael schrieb:
< ...
odbc_fetch_row($rs, $row);
$num=odbc_num_rows($rs);
for ($col=1;$col<$num+1;$col++) {
echo odbc_result($rs, $col);
echo " ";
}

Acutally I only get one row....Thanks indeed.
...


That's right. You can get only one field in one row with odbc_result().
It returns the specified FIELD of the actual ROW. If the field value is
zero or greater than the number of fields an error occures. At least
you have not to count rows (odbc_num_rows()) to get the number of
columns. Use odbc_num_fields() instead.
I seem it's not the badest idea to drop an eye (or two ;-) into the
function reference at the php documentation.

Regards from Hessen
Roland
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.