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

Foreach displaying data twice

P: n/a
I must be missing something rather obvious. I have the following
snippet of code that echo's my result twice when it should be echoing
just once (only one element in the array). What am I overlooking?
********************************
$OpenQ = mysql_query("SELECT column FROM table WHERE q_uid =
$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
result

$Qresult = mysql_fetch_array($OpenQ); // Fetch the array

if ($_GET['OQ']) { // If the get variable is recieved
foreach ($Qresult as $Q) {
echo "<br>" . $Q;
}
}

*********************************

TY.

Jun 1 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Akhenaten kirjoitti:
I must be missing something rather obvious. I have the following
snippet of code that echo's my result twice when it should be echoing
just once (only one element in the array). What am I overlooking?
********************************
$OpenQ = mysql_query("SELECT column FROM table WHERE q_uid =
$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
result

$Qresult = mysql_fetch_array($OpenQ); // Fetch the array
This returns an array that has both index and associative keys. Ie. what
you get is array(0=>'something','column'=>'something');
>
if ($_GET['OQ']) { // If the get variable is recieved
foreach ($Qresult as $Q) {
When you loop thru the array, naturally you'll go thru both the index
and the associative key, which both point to the same value. Instead of
foreaching, you could just echo $Qresult[0] or use mysql_fetch_row or
mysql_fetch_assoc.
echo "<br>" . $Q;
}
}

*********************************

TY.

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Jun 1 '07 #2

P: n/a
At Fri, 01 Jun 2007 07:59:11 -0700, Akhenaten let h(is|er) monkeys type:
I must be missing something rather obvious. I have the following
snippet of code that echo's my result twice when it should be echoing
just once (only one element in the array). What am I overlooking?
********************************
$OpenQ = mysql_query("SELECT column FROM table WHERE q_uid =
$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
result
Rami has answered your question. I just wondered whether:
a) userid is a defined constant. If not, it should be in quotes.
b) $_SESSION[userid] (and q_uid) is a numerical or a string. If the
latter, it should be in quotes.

Just my 2cts.

Sh.
Jun 1 '07 #3

P: n/a
Schraalhans Keukenmeester kirjoitti:
At Fri, 01 Jun 2007 07:59:11 -0700, Akhenaten let h(is|er) monkeys type:
>I must be missing something rather obvious. I have the following
snippet of code that echo's my result twice when it should be echoing
just once (only one element in the array). What am I overlooking?
********************************
$OpenQ = mysql_query("SELECT column FROM table WHERE q_uid =
$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
result

Rami has answered your question. I just wondered whether:
a) userid is a defined constant. If not, it should be in quotes.
I used to think this too, but after reading the manual once again
carefully, I discovered that this is not the case. Singlequotes in array
keys aren't required when inside double quotes. It will not look for the
constant, _unless_ you also use curly braces around it.

$foo = "$array[key]"; // This is right, assumes key is a string

$foo = "{$array[key]}"; // this assumes key is a constant, then falls
back to string, so it'll trigger some stupid notice..

$foo = "{$array['key']}"; // This again is right

$foo = "$array['key']"; // quotes without curly braces is wrong, parse
error.

The thing is, if you're calling a multidimensional array, you gotta use
the curly braces, and that forces you to use the single quotes:
$foo = "{$array[0]['key']}";

It's all here in the better book:
http://fi.php.net/manual/en/language.types.string.php

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Jun 1 '07 #4

P: n/a
I have an upload script which I want to use to upload word documents.
This works fine on my Gradwell account. However on the client's
(windows) hosting it fails.

C:\WINDOWS\TEMP\php29D.tmp
Warning: copy(papers/1180718015test.doc) [function.copy]: failed to open
stream: Permission denied in
\\nas01\nas\i\s\isbe2007.org\web\uploaddoc.php on line 22

I emailed support (Namesco) and received this reply
>There is no need to change permissions on Windows hosting. Your files by
default have the permissions to write to directories owned by your own
user.
Any suggestions?

--
Regards,

Geoff Berrow
Jun 1 '07 #5

P: n/a
At Fri, 01 Jun 2007 19:29:46 +0300, Rami Elomaa let h(is|er) monkeys type:
Schraalhans Keukenmeester kirjoitti:
>>********************************
$OpenQ = mysql_query("SELECT column FROM table WHERE q_uid =
$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
result

Rami has answered your question. I just wondered whether:
a) userid is a defined constant. If not, it should be in quotes.

I used to think this too, but after reading the manual once again
carefully, I discovered that this is not the case. Singlequotes in array
keys aren't required when inside double quotes. It will not look for the
constant, _unless_ you also use curly braces around it.

$foo = "$array[key]"; // This is right, assumes key is a string

$foo = "{$array[key]}"; // this assumes key is a constant, then falls
back to string, so it'll trigger some stupid notice..

$foo = "{$array['key']}"; // This again is right

$foo = "$array['key']"; // quotes without curly braces is wrong, parse
error.

The thing is, if you're calling a multidimensional array, you gotta use
the curly braces, and that forces you to use the single quotes:
$foo = "{$array[0]['key']}";

It's all here in the better book:
http://fi.php.net/manual/en/language.types.string.php
Thanks for setting me straight Rami! Just checked and found it works the
other way around as well: if you DO use single quotes around an array
index inside a double-quoted string curly's are required, lest php throws
an error.
$arr = array('foo'=>'bar');
echo "I met my friends in the $arr[foo]";
I met my friends in the bar
echo "I met my friends in the $arr['foo']";
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE,
expecting T_STRING or T_VARIABLE or T_NUM_STRING
echo "I met my friends in the {$arr['foo']}";
I met my friends in the bar

SOmehow I never thought about the reason why I had to em-brace
one-dimensional array references inside "strings" or heredoc, which also
expands variables of course.

I think I prefer consistent use of curly braces in these situations, and
have the same syntax with any kind of array. Personal taste & habit I
guess.

Never too old to learn.
Rgds

--
Schraalhans Keukenmeester - sc*********@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

Jun 1 '07 #6

P: n/a
Geoff Berrow wrote:
I have an upload script which I want to use to upload word documents.
This works fine on my Gradwell account. However on the client's
(windows) hosting it fails.

C:\WINDOWS\TEMP\php29D.tmp
Warning: copy(papers/1180718015test.doc) [function.copy]: failed to open
stream: Permission denied in
\\nas01\nas\i\s\isbe2007.org\web\uploaddoc.php on line 22

I emailed support (Namesco) and received this reply
>>There is no need to change permissions on Windows hosting. Your files by
default have the permissions to write to directories owned by your own
user.

Any suggestions?
They don't have any idea what they're talking about? You may be able to
write to them as your user - but you need to be able to write them them
as the web server's user. Completely separate requirements.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 2 '07 #7

P: n/a
Message-ID: <wt******************************@comcast.comfro m Jerry
Stuckle contained the following:
>Any suggestions?

They don't have any idea what they're talking about? You may be able to
write to them as your user - but you need to be able to write them them
as the web server's user. Completely separate requirements.
Thanks Jerry, I suspected I was being fobbed off and told them so.

An equally unhelpful reply followed:-

"Dear customer,

The script might work elsewhere, but that is on a different server with
a different configuration. We will not assist you with troubleshooting
your scripts. You will need to establish the cause of the problem
yourself and resolve it accordingly. "

Perhaps they just don't want the business?
--
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/
Jun 2 '07 #8

P: n/a
Try www.technetcenter.com - they aren't perfect, but they are very
helpful and the hosting packages are really great.

T

Geoff Berrow wrote:
Message-ID: <wt******************************@comcast.comfro m Jerry
Stuckle contained the following:
>>Any suggestions?
They don't have any idea what they're talking about? You may be able to
write to them as your user - but you need to be able to write them them
as the web server's user. Completely separate requirements.

Thanks Jerry, I suspected I was being fobbed off and told them so.

An equally unhelpful reply followed:-

"Dear customer,

The script might work elsewhere, but that is on a different server with
a different configuration. We will not assist you with troubleshooting
your scripts. You will need to establish the cause of the problem
yourself and resolve it accordingly. "

Perhaps they just don't want the business?

Jun 2 '07 #9

P: n/a
Geoff Berrow wrote:
Message-ID: <wt******************************@comcast.comfro m Jerry
Stuckle contained the following:
>>Any suggestions?
They don't have any idea what they're talking about? You may be able to
write to them as your user - but you need to be able to write them them
as the web server's user. Completely separate requirements.

Thanks Jerry, I suspected I was being fobbed off and told them so.

An equally unhelpful reply followed:-

"Dear customer,

The script might work elsewhere, but that is on a different server with
a different configuration. We will not assist you with troubleshooting
your scripts. You will need to establish the cause of the problem
yourself and resolve it accordingly. "

Perhaps they just don't want the business?

It sure looks like it. I'd write them back and tell them that since
they can't configure their servers properly and don't even want to know
they have a problem, you're moving to another hosting company.

And then I'd go to www.webhostingtalk.com and let people know about this
company (including name). We don't need any more incompetent, uncaring
hosting companies.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 2 '07 #10

P: n/a
Message-ID: <MM******************@bignews3.bellsouth.netfrom tomb
contained the following:
>Try www.technetcenter.com - they aren't perfect, but they are very
helpful and the hosting packages are really great.
Changing to a more helpful host is probably the best idea but it's not
really an option at the moment.
--
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/
Jun 2 '07 #11

P: n/a
Geoff Berrow wrote:
Message-ID: <MM******************@bignews3.bellsouth.netfrom tomb
contained the following:
>Try www.technetcenter.com - they aren't perfect, but they are very
helpful and the hosting packages are really great.

Changing to a more helpful host is probably the best idea but it's not
really an option at the moment.
Changing hosts is *always* an option.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 2 '07 #12

P: n/a
At Sat, 02 Jun 2007 10:57:13 +0100, Geoff Berrow let h(is|er) monkeys
type:
Message-ID: <wt******************************@comcast.comfro m Jerry
Stuckle contained the following:
>>Any suggestions?
Thanks Jerry, I suspected I was being fobbed off and told them so.

An equally unhelpful reply followed:-

"Dear customer,

The script might work elsewhere, but that is on a different server with
a different configuration. We will not assist you with troubleshooting
your scripts. You will need to establish the cause of the problem
yourself and resolve it accordingly. "
Aha, the typical "Not my problem" approach. Goes hand in hand with "We
don't make any mistakes. Your fault". Change hosts. ASAP. Waste of money &
energy. And tell them why you leave. And make sure you tell everyone about
this utterly poor service. Take your losses, and if they sold you a
product including service, just don't pay for what they don't deliver.

Good luck.
--
Schraalhans Keukenmeester - sc*********@the.Spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') < 0"

Jun 2 '07 #13

P: n/a
Geoff Berrow kirjoitti:
I have an upload script which I want to use to upload word documents.
This works fine on my Gradwell account. However on the client's
(windows) hosting it fails.

C:\WINDOWS\TEMP\php29D.tmp
Warning: copy(papers/1180718015test.doc) [function.copy]: failed to open
stream: Permission denied in
\\nas01\nas\i\s\isbe2007.org\web\uploaddoc.php on line 22

I emailed support (Namesco) and received this reply
>>There is no need to change permissions on Windows hosting. Your files by
default have the permissions to write to directories owned by your own
user.

Any suggestions?
Did you try move_uploaded_file() instead of copy()? If the host has
configured php to safe mode, this might make a difference.

It also might be that the host company are wankers who don't know their
heads from their asses, but the basic thing is that when you handle
uploaded files, you should always use move_uploaded_file() instead of
copy, and it's worth testing at least.

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
Jun 2 '07 #14

P: n/a
Message-ID: <x4******************************@comcast.comfro m Jerry
Stuckle contained the following:
>Changing to a more helpful host is probably the best idea but it's not
really an option at the moment.

Changing hosts is *always* an option.
It may come to that but the possible disruption would not be welcome at
the moment as the site is in active use.

--
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/
Jun 2 '07 #15

P: n/a
Geoff Berrow wrote:
Message-ID: <x4******************************@comcast.comfro m Jerry
Stuckle contained the following:
>>Changing to a more helpful host is probably the best idea but it's not
really an option at the moment.
Changing hosts is *always* an option.

It may come to that but the possible disruption would not be welcome at
the moment as the site is in active use.
And when will it never be "in active use"? Once a site is exposed to
the internet, it's ALWAYS considered active.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 3 '07 #16

P: n/a
Message-ID: <a7***************************@news.speedlinq.nlfr om
Schraalhans Keukenmeester contained the following:
>The script might work elsewhere, but that is on a different server with
a different configuration. We will not assist you with troubleshooting
your scripts. You will need to establish the cause of the problem
yourself and resolve it accordingly. "

Aha, the typical "Not my problem" approach. Goes hand in hand with "We
don't make any mistakes. Your fault". Change hosts. ASAP. Waste of money &
energy. And tell them why you leave. And make sure you tell everyone about
this utterly poor service. Take your losses, and if they sold you a
product including service, just don't pay for what they don't deliver.
I've had an apology and personal phone call from a senior developer.
They are blaming on 'a bug in FrontPage that causes it to remove the
permissions required for uploading from a script when it is installed'.

In addition we are also moving the site to Linux hosting as soon as
possible.

So, a result. Thanks for all your help and support guys.

--
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/
Jun 4 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.