473,231 Members | 1,814 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,231 software developers and data experts.

Can't figure out the error.

Here is some code I can't find the error in. I'm trying to fill a <select>
with option coming from a database, and would like to select a previously
selected record. For this, I set the value $City with the code selected
before. For testing purpose, I set the $City value to 0.
Now, I've 3 records in my table. ZIP fields are: 1950,1,a
When I run the code below, $value becomes "a".
That means that a = = 0; ??? but why ????

function showmessage($text){
echo("<script>alert('".$text."');</script>"); //debug function
}

$City = 0; //for testing purpose
$query_RSCity = "SELECT DISTINCT ZIP, Cityname From person where
person.IDType > 1;";
if(($RSCity = mysql_query($query_RSCity, $shopauto))){
while($QRow = mysql_fetch_object($RSCity)){
echo("<OPTION VALUE=\"".$QRow->ZIP."\"");
if ($QRow->ZIP == $City){
echo(" selected ");
$value = 'test';
}
echo(">".$QRow->ZIP."&nbsp;".$QRow->Cityname."</OPTION>\n");
if($value <> "")
showmessage("City: ".$City.", ZIP: ".$QRow->ZIP);
}
}

Bob
Jul 17 '05 #1
1 1261
On Wed, 10 Nov 2004 22:07:18 +0100, "Bob Bedford"
<be******@YouKnowWhatToDoHerehotmail.com> wrote:
Here is some code I can't find the error in. I'm trying to fill a <select>
with option coming from a database, and would like to select a previously
selected record. For this, I set the value $City with the code selected
before. For testing purpose, I set the $City value to 0.
Now, I've 3 records in my table. ZIP fields are: 1950,1,a
When I run the code below, $value becomes "a".
That means that a = = 0; ??? but why ????
'a' == 0 is true. This is because of type juggling. See:

http://www.php.net/manual/en/languag...e-juggling.php

Non-numeric strings implicitly convert to zero integers. When trying to
compare a string with an integer, the left hand expression is converted to the
type of the right hand expression; 'a' becomes 0, which matches 0.

If you want to suppress type juggling, use the === operator.

== is "both operand expressions are equivalent when converted to a common
type"

=== is "both operand expressions are equivalent in both type and value"

Consider the output of:

<table>
<?php
$values = array('0', '1', 'a', '', 0, 1, null, false, true);

for ($i=0; $i<count($values); $i++)
{
for ($j=0; $j<count($values); $j++)
{
if ($i != $j)
{
print "<tr><td>";
var_dump($values[$i]);
print "</td><td>==</td><td>";
var_dump($values[$j]);
print "</td><td>";
print "is ";
var_dump($values[$i] == $values[$j]);
print "</td><td>&nbsp;</td>";

print "<td>";
var_dump($values[$i]);
print "</td><td>===</td><td>";
var_dump($values[$j]);
print "</td><td>";
print "is ";
var_dump($values[$i] === $values[$j]);
print "</td></tr>";

}
}
}
?>
</table>
function showmessage($text){
echo("<script>alert('".$text."');</script>"); //debug function
}

$City = 0; //for testing purpose
$query_RSCity = "SELECT DISTINCT ZIP, Cityname From person where
person.IDType > 1;";
if(($RSCity = mysql_query($query_RSCity, $shopauto))){
while($QRow = mysql_fetch_object($RSCity)){
echo("<OPTION VALUE=\"".$QRow->ZIP."\"");
if ($QRow->ZIP == $City){
echo(" selected ");
$value = 'test';
You said:
When I run the code below, $value becomes "a".


But the line above is the only time $value is written to, and you put 'test'
into it. So if $value becomes 'a' it's being done by code you haven't posted.

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

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

Similar topics

4
by: Aaron Walker | last post by:
Greetings, I'm attempting to write my first *real* template function that also deals with a map of strings to member function pointers that is making the syntax a little tricky to get right. ...
6
by: Frankie Montenegro | last post by:
Hi everyone, I need some help with the following C++ code. It is supposed to read input line by line, each line as one string; then split each string into its component words and store these...
10
by: MHenry | last post by:
Hi, We were going merrily along for 6 years using this database to record all client checks that came into our office, including information about what the checks were for. Suddenly, network...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
0
by: William Stacey [MVP] | last post by:
This code worked on fx 1.1 and now I get a "Keyset does not exist" error when trying to SignData with RSA under FX2.0. Smells like some security error, but can't debug it as I think error is...
3
by: Brian Blais | last post by:
Hello, I have an odd kind of Heisenbug in what looks like a pretty simple program. The program is a progress bar code I got at the Python Cookbook: ...
2
by: kkleung89 | last post by:
Basically, here's what's happening with the program. I have a table of Customers and a table of Pets, with the latter containing a field linking it to its customer of ownership. I have a form...
9
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. For a...
2
by: ayman723 | last post by:
hi; I have this code in my book and when I copied it into my compiler into two files, one is header and the other is .cpp. it gives me an error, I'm sure I copied it right but can't figure out the...
4
by: Protoman | last post by:
Can you please help me figure out what the error is here, in this rotor cipher simulator, which I wrote to amuse myself? The runtime error is that it isn't symmetrical --decrypting a piece of...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.