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
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > Tools > Zend Studio

Zend Studio Looking for help with Zend Studio? This is the place.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-28-2005, 03:05 PM   #1
jtp51
Member
 
jtp51's Avatar
 
Join Date: Mar 2003
Location: Omaha, Nebraska
Posts: 64
Question Zend IDE: Assignment in condition Error Msg

While this is perfectly exceptable:

Code:
while ($row = mysql_fetch_row($result)) {
	$num_rows = mysql_num_rows($result);
	for ($i = 0; $i < $num_rows; $i++) {
		$tab = mysql_tablename($result, $i);
		if(strtolower(trim($tab)) === 'table_name')
		{
			$run_job = true;
		}
	}
}
Zend Studio IDE keeps returning the analysis:

BUG: Assignment in condition

Even though the above code is used as an example... Anyone else run into this?

Thanks,
__________________
--Todd

"You're progam was throwing Null Pointer Exceptions like rice at a wedding..."
jtp51 is offline   Reply With Quote
Old 10-28-2005, 04:16 PM   #2
unreal128
Member
 
Join Date: Apr 2005
Posts: 71
I am taking a stab but I think it doesn't like...
PHP Code:
while ($row = mysql_fetch_row($result))
...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.
unreal128 is offline   Reply With Quote
Old 05-18-2007, 06:58 PM   #3
Jaxolotl
Junior Member
 
Join Date: May 2007
Posts: 2
(...two years later..) hehe

Try this it should work
PHP Code:
$row = mysql_fetch_row($result);
while (
$row) {
        
$row = mysql_fetch_row($result);
    
$num_rows = mysql_num_rows($result);
    for (
$i = 0; $i < $num_rows; $i++) {
        
$tab = mysql_tablename($result, $i);
        if(
strtolower(trim($tab)) === 'table_name')
        {
            
$run_job = true;
        }
    }
}

Last edited by Jaxolotl; 05-18-2007 at 07:00 PM.
Jaxolotl is offline   Reply With Quote
Old 05-20-2007, 08:09 AM   #4
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,402
Quote:
(...two years later..) hehe
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.
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-21-2007, 10:55 AM   #5
Jaxolotl
Junior Member
 
Join Date: May 2007
Posts: 2
Quote:
Originally Posted by laserlight
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)
Jaxolotl is offline   Reply With Quote
Old 05-30-2007, 09:48 AM   #6
faifas
Member
 
Join Date: Jul 2006
Posts: 40
Quote:
Originally Posted by Jaxolotl
(...two years later..) hehe

Try this it should work
PHP Code:
$row = mysql_fetch_row($result);
[
b]while ($row) {
        
$row = mysql_fetch_row($result);[/b]
    
$num_rows = mysql_num_rows($result);
    for (
$i = 0; $i < $num_rows; $i++) {
        
$tab = mysql_tablename($result, $i);
        if(
strtolower(trim($tab)) === 'table_name')
        {
            
$run_job = true;
        }
    }
}
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
faifas is offline   Reply With Quote
Old 04-29-2008, 03:14 AM   #7
bobvandell
Junior Member
 
Join Date: Apr 2008
Posts: 1
Here is how you should use it:

PHP Code:
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.
bobvandell is offline   Reply With Quote
Old 04-30-2008, 01:17 PM   #8
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,402
Quote:
Here is how you should use it:
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.

Have a great day
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 04-30-2008, 10:07 PM   #9
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
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.
Weedpacket is offline   Reply With Quote
Old 05-01-2008, 12:20 AM   #10
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,402
Quote:
Bobvandell's trick is in the double parentheses.
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).
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-02-2008, 03:02 AM   #11
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
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.
Weedpacket is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 06:53 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.