473,405 Members | 2,294 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,405 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 1264
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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...

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.