While this is perfectly exceptable:

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:

[FONT=Courier New]BUG: Assignment in condition[/FONT]

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

Thanks,

    I am taking a stab but I think it doesn't like...

    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.

      2 years later

      (...two years later..) hehe

      Try this it should work

      $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;
      		}
      	}
      }
      

        (...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.

          laserlight wrote:

          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

          
          // 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)

            9 days later
            Jaxolotl wrote:

            (...two years later..) hehe

            Try this it should work

            $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;
            		}
            	}
            }
            

            :o people, omtimize your code,
            what's the point of numbering rows all the time? :o
            Number your rows and then do while() though I'm not sure if you need it 😛

              a year later
              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 😛

              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.

                Here is how you should use it:

                Remarkably, that looks very much like the code snippet posted by jtp51.

                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").

                Very simple and efficient. Have a great day 😛

                Simple indeed. You could be more efficient if you actually read the thread before replying.

                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 :p

                  laserlight wrote:

                  Remarkably, that looks very much like the code snippet posted by jtp51.

                  Bobvandell's trick is in the double parentheses.

                  while(($thing=$foo))

                  as opposed to

                  while($thing=$foo)

                  ZDE warns about "while(assignment expression)", but "(assignment expression)" is not itself an assignment expression, so ZDE remains silent.

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

                    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).

                      laserlight wrote:

                      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?).

                      laserlight wrote:

                      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.

                        4 years later

                        WILL YOU SHUT THE fack up laserlight !

                        You are fackin useless in that thread !

                          Write a Reply...