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

equality and TINYINT field?

P: n/a
Hello

I have a TINYINT(1) field, group1, in a mysql data base and the value
is 1 but php

if ($row['group1'] == "1") {

does not work.

What should I be writing here?

Cheers

Geoff

PS echoing the group1 value does give 1.
Aug 12 '08 #1
Share this Question
Share on Google+
21 Replies


P: n/a
On Aug 12, 9:16*am, Geoff Cox <g...@freeuk.notcomwrote:
the value is 1 but php
if ($row['group1'] == "1") {
does not work.
This should work. However, if $row['group1'] contains "1 " (one space)
it does not work. You can use vardump($row['group1']) to determine the
precise contents and type of the variable.

What also could be the case is that the if works, but you think it is
not because something else is broken.
Aug 12 '08 #2

P: n/a
Jim
Geoff Cox wrote:
Hello

I have a TINYINT(1) field, group1, in a mysql data base and the value
is 1 but php

if ($row['group1'] == "1") {

does not work.

What should I be writing here?

Cheers

Geoff

PS echoing the group1 value does give 1.
Lose the double quotes round 1 ?

-- Jim.
Aug 12 '08 #3

P: n/a
On Tue, 12 Aug 2008 00:35:05 -0700 (PDT), Sjoerd <sj******@gmail.com>
wrote:
>On Aug 12, 9:16*am, Geoff Cox <g...@freeuk.notcomwrote:
>the value is 1 but php
if ($row['group1'] == "1") {
does not work.

This should work. However, if $row['group1'] contains "1 " (one space)
it does not work. You can use vardump($row['group1']) to determine the
precise contents and type of the variable.
This is odd! I am using var_dump and code below which first gives
group1 as 1 and then var_dump as NULL and then no.

any idea what is going on?!

Cheers

Geoff

while ($row = mysql_fetch_array($result)) {
echo $row['user_name'] . " "
.. $row['group1'] ." "
.. $row['group2'] . " "
.. $row['group3'] . " "
.. $row['group4'] . " "
.. $row['group5']
.."<br>";
}

echo var_dump($row['group1']);

if ($row['group1'] == 1) {
echo "yes" . "<br>";
} else {
echo "no" . "<br>";
}


>
What also could be the case is that the if works, but you think it is
not because something else is broken.
Aug 12 '08 #4

P: n/a
On Tue, 12 Aug 2008 08:41:48 +0100, Jim <no*@this.addresswrote:
>Geoff Cox wrote:
>Hello

I have a TINYINT(1) field, group1, in a mysql data base and the value
is 1 but php

if ($row['group1'] == "1") {

does not work.

What should I be writing here?

Cheers

Geoff

PS echoing the group1 value does give 1.

Lose the double quotes round 1 ?

-- Jim.

I've tried that Jim - please have a look at my reply to Sjoerd and see
if you have any thoughts!

Cheers

Geoff
Aug 12 '08 #5

P: n/a
Geoff Cox wrote:
On Tue, 12 Aug 2008 00:35:05 -0700 (PDT), Sjoerd <sj******@gmail.com>
wrote:
>>On Aug 12, 9:16 am, Geoff Cox <g...@freeuk.notcomwrote:
>>the value is 1 but php
if ($row['group1'] == "1") {
does not work.

This should work. However, if $row['group1'] contains "1 " (one space)
it does not work. You can use vardump($row['group1']) to determine the
precise contents and type of the variable.

This is odd! I am using var_dump and code below which first gives
group1 as 1 and then var_dump as NULL and then no.
Please post the output from
var_dump($row)
Aug 12 '08 #6

P: n/a
On Tue, 12 Aug 2008 10:00:18 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:
>Geoff Cox wrote:
>On Tue, 12 Aug 2008 00:35:05 -0700 (PDT), Sjoerd <sj******@gmail.com>
wrote:
>>>On Aug 12, 9:16 am, Geoff Cox <g...@freeuk.notcomwrote:
the value is 1 but php
if ($row['group1'] == "1") {
does not work.

This should work. However, if $row['group1'] contains "1 " (one space)
it does not work. You can use vardump($row['group1']) to determine the
precise contents and type of the variable.

This is odd! I am using var_dump and code below which first gives
group1 as 1 and then var_dump as NULL and then no.

Please post the output from
var_dump($row)
Paul,

not much to tell (if I understand you correctly) - I get

fred 1 0 0 0 0
NULL
no

where NULL refers to $row['group1'] which in the first line is 1?!

Cheers

Geoff

PS the code is

echo var_dump($row['group1']) . "<br>";
if ($row['group1'] == 1) {
echo "yes" . "<br>";
} else {
echo "no" . "<br>";
}

} else {
echo mysql_error();
}
Aug 12 '08 #7

P: n/a
Geoff Cox wrote:
On Tue, 12 Aug 2008 10:00:18 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:
>>Geoff Cox wrote:
>>On Tue, 12 Aug 2008 00:35:05 -0700 (PDT), Sjoerd
<sj******@gmail.comwrote:

On Aug 12, 9:16 am, Geoff Cox <g...@freeuk.notcomwrote:
the value is 1 but php
if ($row['group1'] == "1") {
does not work.

This should work. However, if $row['group1'] contains "1 " (one
space) it does not work. You can use vardump($row['group1']) to
determine the precise contents and type of the variable.

This is odd! I am using var_dump and code below which first gives
group1 as 1 and then var_dump as NULL and then no.

Please post the output from
var_dump($row)

Paul,

not much to tell (if I understand you correctly) - I get

fred 1 0 0 0 0
NULL
no

where NULL refers to $row['group1'] which in the first line is 1?!

Cheers

Geoff

PS the code is

echo var_dump($row['group1']) . "<br>";
if ($row['group1'] == 1) {
echo "yes" . "<br>";
} else {
echo "no" . "<br>";
}

} else {
echo mysql_error();
}
I asked:
Please post the output from
var_dump($row)

you posted something else.

Let me try this again:
Please post the output from
var_dump($row)

(Hint, now it is your turn. What you need to do is to READ what I asked and
to DO what is asked. This is how you get help!)
Aug 12 '08 #8

P: n/a
On Tue, 12 Aug 2008 13:26:50 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:

>>
echo var_dump($row['group1']) . "<br>";
if ($row['group1'] == 1) {
echo "yes" . "<br>";
} else {
echo "no" . "<br>";
}

} else {
echo mysql_error();
}

I asked:
Please post the output from
var_dump($row)

you posted something else.

Let me try this again:
Please post the output from
var_dump($row)
Paul,

'sorry - kind of watch my lips stupid!

I get

bool(false)

Cheers,

Geoff
Aug 12 '08 #9

P: n/a
On Tue, 12 Aug 2008 13:26:50 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:

>(Hint, now it is your turn. What you need to do is to READ what I asked and
to DO what is asked. This is how you get help!)

Paul

this is how I created the table

CREATE TABLE best_users (
user_name text,
group1 tinyint(1),
group2 tinyint(1),
group3 tinyint(1),
group4 tinyint(1),
group5 tinyint(1)
) TYPE=MyISAM;

--
-- Dumping data for table best_docs
--

INSERT INTO best_users VALUES ('fred',1,0,0,0,0);
INSERT INTO best_users VALUES ('jill',0,1,0,0,1);
INSERT INTO best_users VALUES ('jane',1,0,0,0,0);
INSERT INTO best_users VALUES ('sam',0,1,0,0,1);
INSERT INTO best_users VALUES ('bill',0,0,0,1,1);
INSERT INTO best_users VALUES ('mary',0,0,1,0,0);

any clue there?

Cheers

Geoff

PS is using SSH I do a

select * from best_users where group1 = 1

this works OK and I get records 1 and 3 as I ought....
Aug 12 '08 #10

P: n/a
Geoff Cox wrote:
On Tue, 12 Aug 2008 13:26:50 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:

>>>
echo var_dump($row['group1']) . "<br>";
if ($row['group1'] == 1) {
echo "yes" . "<br>";
} else {
echo "no" . "<br>";
}

} else {
echo mysql_error();
}

I asked:
Please post the output from
var_dump($row)

you posted something else.

Let me try this again:
Please post the output from
var_dump($row)

Paul,

'sorry - kind of watch my lips stupid!

I get

bool(false)

Cheers,

Geoff
You need to execute:
$row = mysql_fetch_array($result))
var_dump($row)

You do not use echo with var_dump
Aug 12 '08 #11

P: n/a
On Tue, 12 Aug 2008 14:44:29 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:

>You need to execute:
$row = mysql_fetch_array($result))
var_dump($row)

You do not use echo with var_dump

OK I see - I get

array(12) { [0]= string(4) "fred" ["user_name"]= string(4) "fred"
[1]= string(1) "1" ["group1"]= string(1) "1" [2]= string(1) "0"
["group2"]= string(1) "0" [3]= string(1) "0" ["group3"]=>
string(1) "0" [4]= string(1) "0" ["group4"]= string(1) "0" [5]=>
string(1) "0" ["group5"]= string(1) "0" }

Cheers

Geoff


Aug 12 '08 #12

P: n/a
On Tue, 12 Aug 2008 14:44:29 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:
>Geoff Cox wrote:
>On Tue, 12 Aug 2008 13:26:50 +0100, "Paul Lautman"
<pa**********@btinternet.comwrote:

>>>>
echo var_dump($row['group1']) . "<br>";
if ($row['group1'] == 1) {
echo "yes" . "<br>";
} else {
echo "no" . "<br>";
}

} else {
echo mysql_error();
}

I asked:
Please post the output from
var_dump($row)

you posted something else.

Let me try this again:
Please post the output from
var_dump($row)

Paul,

'sorry - kind of watch my lips stupid!

I get

bool(false)

Cheers,

Geoff

You need to execute:
$row = mysql_fetch_array($result))
var_dump($row)

You do not use echo with var_dump
Paul,

I have found out how to get the

if ($row['group1'] == 1) { etc

to work but perhaps you can explain why!?

If I make the test within the while loop,

while ($row = mysql_fetch_array($result)) {
($row['group1'] == 1) { etc

this works.

Outside the loop $row['group1'] is empty (or NULL?)

Why is this?

Cheers

Geoff

Aug 13 '08 #13

P: n/a
On 13 Aug, 08:40, Geoff Cox <g...@freeuk.notcomwrote:
On Tue, 12 Aug 2008 14:44:29 +0100, "Paul Lautman"

<paul.laut...@btinternet.comwrote:
Geoff Cox wrote:
On Tue, 12 Aug 2008 13:26:50 +0100, "Paul Lautman"
<paul.laut...@btinternet.comwrote:
>>echo var_dump($row['group1']) . "<br>";
if ($row['group1'] == 1) {
echo "yes" . "<br>";
} else {
echo "no" . "<br>";
}
>>} else {
echo mysql_error();
}
>>I asked:
Please post the output from
var_dump($row)
>>you posted something else.
>>Let me try this again:
Please post the output from
var_dump($row)
Paul,
'sorry - kind of watch my lips stupid!
I get
bool(false)
Cheers,
Geoff
You need to execute:
$row = mysql_fetch_array($result))
var_dump($row)
You do not use echo with var_dump

Paul,

I have found out how to get the

if ($row['group1'] == 1) { etc

to work but perhaps you can explain why!?

If I make the test within the while loop,

while ($row = mysql_fetch_array($result)) {
($row['group1'] == 1) { etc

this works.

Outside the loop $row['group1'] is empty (or NULL?)

Why is this?

Cheers

Geoff- Hide quoted text -

- Show quoted text -
$row doesn't get a value until you assign one.
So if you do:
$row = mysql_fetch_array($result))
echo "@{$row['group1']}@"
Aug 13 '08 #14

P: n/a
Message-ID: <fo********************************@4ax.comfrom Geoff Cox
contained the following:
>
while ($row = mysql_fetch_array($result)) {
($row['group1'] == 1) { etc

this works.

Outside the loop $row['group1'] is empty (or NULL?)

Why is this?
<fx: smacks head>
For every iteration of $row = mysql_fetch_array($result) then
$row['group1'] will have a different value corresponding to the entry in
the database for that particular row. Outside the loop (below the while
loop in the code) $row['group1'] will have the value corresponding to
the last row of the database query.

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk
Aug 13 '08 #15

P: n/a
On Wed, 13 Aug 2008 02:58:39 -0700 (PDT), Captain Paralytic
<pa**********@yahoo.comwrote:

>
$row doesn't get a value until you assign one.
So if you do:
$row = mysql_fetch_array($result))
echo "@{$row['group1']}@"
OK - get it.

Thanks

Geoff
Aug 13 '08 #16

P: n/a
On Wed, 13 Aug 2008 11:05:18 +0100, Geoff Berrow
<bl******@ckdog.co.ukwrote:
>Message-ID: <fo********************************@4ax.comfrom Geoff Cox
contained the following:
>>
while ($row = mysql_fetch_array($result)) {
($row['group1'] == 1) { etc

this works.

Outside the loop $row['group1'] is empty (or NULL?)

Why is this?

<fx: smacks head>
For every iteration of $row = mysql_fetch_array($result) then
$row['group1'] will have a different value corresponding to the entry in
the database for that particular row. Outside the loop (below the while
loop in the code) $row['group1'] will have the value corresponding to
the last row of the database query.

Thanks - it came to me on the way home just now on my bike!

Cheers

Geoff
Aug 13 '08 #17

P: n/a
Geoff Berrow wrote:
Message-ID: <fo********************************@4ax.comfrom Geoff Cox
contained the following:
>while ($row = mysql_fetch_array($result)) {
($row['group1'] == 1) { etc

this works.

Outside the loop $row['group1'] is empty (or NULL?)

Why is this?

<fx: smacks head>
For every iteration of $row = mysql_fetch_array($result) then
$row['group1'] will have a different value corresponding to the entry in
the database for that particular row. Outside the loop (below the while
loop in the code) $row['group1'] will have the value corresponding to
the last row of the database query.
No it wont. It wil have a NULL value assigned, as it is the failure of
$row to be anything other than null that *causes the loop to exit*.

Aug 13 '08 #18

P: n/a
Message-ID: <12****************@proxy00.news.clara.netfrom The Natural
Philosopher contained the following:
><fx: smacks head>
For every iteration of $row = mysql_fetch_array($result) then
$row['group1'] will have a different value corresponding to the entry in
the database for that particular row. Outside the loop (below the while
loop in the code) $row['group1'] will have the value corresponding to
the last row of the database query.
No it wont. It wil have a NULL value assigned, as it is the failure of
$row to be anything other than null that *causes the loop to exit*.
Hmm, good point. In practice you wouldn't normally have a use for $row
outside the loop so it's never an issue.
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk
Aug 13 '08 #19

P: n/a
On Wed, 13 Aug 2008 19:45:58 +0100, The Natural Philosopher <a@b.c>
wrote:
>Geoff Berrow wrote:
>Message-ID: <fo********************************@4ax.comfrom Geoff Cox
contained the following:
>>while ($row = mysql_fetch_array($result)) {
($row['group1'] == 1) { etc

this works.

Outside the loop $row['group1'] is empty (or NULL?)

Why is this?

<fx: smacks head>
For every iteration of $row = mysql_fetch_array($result) then
$row['group1'] will have a different value corresponding to the entry in
the database for that particular row. Outside the loop (below the while
loop in the code) $row['group1'] will have the value corresponding to
the last row of the database query.
No it wont. It wil have a NULL value assigned, as it is the failure of
$row to be anything other than null that *causes the loop to exit*.
in fact that's what I found - a null value - which made me think I
need to test within the while loop.

Cheers

Geoff
Aug 13 '08 #20

P: n/a
Message-ID: <jh********************************@4ax.comfrom Geoff Cox
contained the following:
>in fact that's what I found - a null value - which made me think I
need to test within the while loop.
You're still not getting it are you?
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk
Aug 13 '08 #21

P: n/a
Geoff Cox wrote:
On Wed, 13 Aug 2008 19:45:58 +0100, The Natural Philosopher <a@b.c>
wrote:
>Geoff Berrow wrote:
>>Message-ID: <fo********************************@4ax.comfrom Geoff Cox
contained the following:

while ($row = mysql_fetch_array($result)) {
($row['group1'] == 1) { etc

this works.

Outside the loop $row['group1'] is empty (or NULL?)

Why is this?
<fx: smacks head>
For every iteration of $row = mysql_fetch_array($result) then
$row['group1'] will have a different value corresponding to the entry in
the database for that particular row. Outside the loop (below the while
loop in the code) $row['group1'] will have the value corresponding to
the last row of the database query.
No it wont. It wil have a NULL value assigned, as it is the failure of
$row to be anything other than null that *causes the loop to exit*.

in fact that's what I found - a null value - which made me think I
need to test within the while loop.
Indeed. That is exactly what the code should give you: a null result.

I am not sure what you were TRYING to achieve with that code fragment,
but the problem has nothing to do with equality and tinyints..
Cheers

Geoff
Aug 14 '08 #22

This discussion thread is closed

Replies have been disabled for this discussion.