473,413 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

foreach

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
7 2165
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
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
*** 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
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
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
*** 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
8
by: cody | last post by:
currently, foreach takes a IEnumerable as parameter, so we can do: foreach (int i in array){} what about an additional form of foreach that takes an IEnumerator as parameter, so we can do: ...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.