To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
...since you are assigning the results of mysql_fetch_rows to $row and it is also enclosed in a conditional if statement. Its puzzling though since this is a pretty common "if" mySQL statement.
Precisely, there was no need to engage in thread necromancy, especially when your proposed solution is wrong anyway. The second assignment to $row should happen at the end of the while loop's body, not at the beginning, otherwise the first row in the result set will be skipped.
But yeah, it looks like a non-bug by an overzealous IDE (setting?) in the first place.
Precisely, there was no need to engage in thread necromancy, especially when your proposed solution is wrong anyway. The second assignment to $row should happen at the end of the while loop's body, not at the beginning, otherwise the first row in the result set will be skipped.
I'm sorry about that, I was very tired that day, anyway going into thread necromancy (I like the way you call it ) soetimes helps other people but the thread author.
Here the code without errors
PHP Code:
// my query
$query = "SELECT `field` FROM `table`";
// my db abstraction
sql_select($query,$results);
// This fetch the first data of the resourse into an array
$my_row = mysql_fetch_array($results);
while($my_row){
// DO SOMETHING
// once the first data row is used it gets the next one if available so it MUST be at the end of the cycle
$my_row = mysql_fetch_array($results);
}
Anyway I didn't decide which way is better because assignments in condition generally improve readability even though it's not considered a good "style" practice. Think it depend on the situation, if you ar just assigning the row into an array and nothing more it would be better to do it inside the condition, but if you are doing more then just an assignment it would be better to skip the assignment in condition just to prevent cofunsion (but it's just my oppinion)
people, omtimize your code,
what's the point of numbering rows all the time?
Number your rows and then do while() though I'm not sure if you need it :P
while (($row = mysql_fetch_array($result))) { // Code goes here. }
The above evaluates the assignment using the extra parenthesis "()" around it.
This returns to the while loop a value of true or false (boolean).
Very simple and efficient. Have a great day :P
Also the comment posted saying "Try this it should work":
I'm pretty sure that by the time you enter the while loop, the row pointer has already been established before entering the loop on the result set, and the second instance of using $row = mysql_fetch_row($result) within the loop will move it to the next row before you've had a chance to use your $row variable. Just a heads up.
Last edited by bobvandell; 04-29-2008 at 03:17 AM.
Remarkably, that looks very much like the code snippet posted by jtp51.
Quote:
The above evaluates the assignment using the extra parenthesis "()" around it.
This returns to the while loop a value of true or false (boolean).
Of course, your explanation is unnecessary since jtp51 obviously understands how it works, and could even comment that "this is perfectly exceptable" (where "exceptable" presumably is intended to mean "acceptable").
Quote:
Very simple and efficient. Have a great day :P
Simple indeed. You could be more efficient if you actually read the thread before replying.
Quote:
I'm pretty sure that by the time you enter the while loop, the row pointer has already been established before entering the loop on the result set, and the second instance of using $row = mysql_fetch_row($result) within the loop will move it to the next row before you've had a chance to use your $row variable. Just a heads up.
That's true, and I pointed it out almost a year ago. Not to mention that jtp51 posted the question in 2005, so you are several years late.
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,137
Quote:
Originally Posted by laserlight
Remarkably, that looks very much like the code snippet posted by jtp51.
Bobvandell's trick is in the double parentheses.
PHP Code:
while(($thing=$foo))
as opposed to
PHP Code:
while($thing=$foo)
ZDE warns about "while(assignment expression)", but "(assignment expression)" is not itself an assignment expression, so ZDE remains silent.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
It's a poor substitute for turning off an unnecessary warning. I suppose there's a case to be made for it to be an explicit statement that the assignment is intentional, but...
Quote:
ZDE warns about "while(assignment expression)", but "(assignment expression)" is not itself an assignment expression, so ZDE remains silent.
... considering that this particular idiom is prevalent in PHP, I still think that the warning is a ZDE bug. If ZDE insists on providing it, it should have a feature to turn the warning off, and state as such in the warning message (like what Microsoft Visual C++ does for the use of standard C functions for which it provides arguably safer alternatives).
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,137
Quote:
Originally Posted by laserlight
considering that this particular idiom is prevalent in PHP, I still think that the warning is a ZDE bug.
It's also a common mistake (how often have you seen posts here where the bug was due to an unintentional assignment?).
Quote:
Originally Posted by laserlight
If ZDE insists on providing it, it should have a feature to turn the warning off
This is available in ZDE Eclipse, but I reckon it should also be available on an instance-by-instance basis; have it turned on, but allow the user to say for each case "no, that's not a mistake, I know what I'm doing kthxbye" and have that recorded in the project.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.