473,785 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Foreach displaying data twice

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("SE LECT column FROM table WHERE q_uid =
$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
result

$Qresult = mysql_fetch_arr ay($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
16 4512
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("SE LECT column FROM table WHERE q_uid =
$_SESSION[userid] AND status = 1",$db); // SQL query on DB returns 1
result

$Qresult = mysql_fetch_arr ay($OpenQ); // Fetch the array
This returns an array that has both index and associative keys. Ie. what
you get is array(0=>'somet hing','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_ass oc.
echo "<br>" . $Q;
}
}

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

TY.

--
Ra*********@gma il.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
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("SE LECT 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
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("SE LECT 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 multidimensiona l 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*********@gma il.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
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\w eb\uploaddoc.ph p 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
At Fri, 01 Jun 2007 19:29:46 +0300, Rami Elomaa let h(is|er) monkeys type:
Schraalhans Keukenmeester kirjoitti:
>>************* *************** ****
$OpenQ = mysql_query("SE LECT 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 multidimensiona l 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'=>'b ar');
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 .Spamtrapexampl e.nl
[Remove the lowercase part of Spamtrap to send me a message]

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

Jun 1 '07 #6
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\w eb\uploaddoc.ph p 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*******@attgl obal.net
=============== ===
Jun 2 '07 #7
Message-ID: <wt************ *************** ***@comcast.com from 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
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.com from 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
Geoff Berrow wrote:
Message-ID: <wt************ *************** ***@comcast.com from 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*******@attgl obal.net
=============== ===
Jun 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2921
by: Mountain Man | last post by:
Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first key for all the keys. My code is shown below and the problem areas are marked with comments. In case you're wondering, this script is for generating sticky checkboxes that include an event handler. Many thanks to anyone who can help.
3
1963
by: pmud | last post by:
Hi, I have 4 columns in my sql database table. I added a data adapter , data set & a data grid to view this information. But the data grid is displaying those 4 columns TWICE , i.e in the data grid i see 8 columns. In case this is useful, the primary key is Serial no. which is an identity ....i.e everytime a row is added to the database, it increments by1. Also, when i configured the data adapter, it couldnt Generate the UPDATE &...
7
5551
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
104
7202
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 foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
2
4359
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control displaying the contents of the data source, whilst another control updates the datasource via a command buttons implementation of 'Click', an event raised in the 'Handle Postback Events' stage of the control execution life cycle (via the...
4
1911
by: Jack | last post by:
Hi, I have the following Access DDL statment from which I generated a access table: CREATE TABLE TEST1 (ID COUNTER, NAME TEXT(50), STORAGESPACEASSIGNED TEXT(50) )
3
3011
by: monomaniac21 | last post by:
//this loop doesnt work as intended, each row displays twice, once with a number as $key and again with key name as $key!? Any ideas why? $result = mysql_query("SELECT * FROM tbl WHERE id = '1' "); while($row = mysql_fetch_array($result)) { foreach ($row as $key =$value) {
0
1200
by: arunpulikkan | last post by:
hi,guys im new to asp.in my option list one value is displaying twice.following is the code if not rs1.eof then thispagesForm = rs1(0) sql2 = "select formname, local_url from xwhere formid = "& thispagesForm set rs2 = conn.execute(sql2) thispagesFormName = rs2(0) local_url = rs2(1)
4
2565
by: Peter Morris | last post by:
I am not sure what you are asking. You seem to be asking how to implement a plain IEnumerable on a composite structure, but then your example shows a flat structure using "yield". Your subject makes me infer that you think foreach is enirely useless for composite structures. I will address the subject text, because that makes the most sense to me :-) Take the following class public class MyTreeNode : IEnumerable<MyTreeNode> {
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.