Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

integer calculation error

Status
Not open for further replies.

LordGarfield

IS-IT--Management
Jul 31, 2003
112
BE
hi,

I have a database with a few fields
menuid int(11) autonumber
name varchar(100)
sorteer int(11)
link varchar(255)

now the strange thing is
I select the sorteer integer out of the database into a var
and I wanne add 1 to it.

<connection>
$sql1 = &quot;select * from menu order by sorteer&quot;;
$rec = mysql_query($sql1);
while(list($id, $name, $sorteer, $link) = mysql_fetch_row($rec))
{
echo $id . &quot;<br>&quot;;
echo $sorteer . &quot;<br>&quot;;
}
$id = $id +1;
echo $id . &quot;<br>&quot;;
$sorteer = $sorteer + 1;
echo $sorteer . &quot;<br>&quot;;

there is only one item in the db but the result is like this

1
1
1
1

so he did not add 1 to the $id var and also not to the $sorteer.

How come???

mvg
Roel.
 
In the future I would recommend using the associative array as returned by mysql_fetch_assoc() instead of the construct you use. Also, do yourself a favor and add error checking for the MySQL commands:
Code:
$SQL = &quot;SELECT * FROM menu ORDER by sorteer&quot;;
$result = mysql_query($SQL) OR die(&quot;Query error: &quot;.mysql_error());

while ($row = mysql_fetch_assoc($result)){
   # you refer to the columns by the array key
   echo($row['sorteer']);
   ...etc...
}

Follow sleipnir214's advice and check out what's in the vars before incrementing.
 
It seems that { } declares scopes like in C++.

He did not know the variable $sorteer anymore outside of the while{}.

verry strange. I thought php did not used that.
 
You are mistaken.
There is no scope declaration like you mention.
$sorteer is not known outside the loop because it is empty when the retrieval of the next row fails.
 
but why if I put it in a session var it will work?
that is the only thing I did. I putted $sorteer in a session var and that is it?

 
A variable within a loop is assigned at each iteration. Don't be fooled by the fact that you have just one record. The loop will run while the expression evaluates as true.
When a record is retrieved the list elements are assigned. If no record is retrieved the list is not assigned any values. That, however, does not mean that the previous value is still in the listed vars.

Sessions is something alltogether different and have no bearing on this. You could have assigned your one record variable to $whatEverName and it would be there. That's the case because, I'm certain, you assign the value within the while loop. With one record it only passed that assignment once. That's why the value is there.
The reason for $sorteer to have no value is in the fact that it is explicitly assigned in the expression of the while loop.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top