dongshanxiao7328
2012-10-05 15:39 阅读 8
已采纳

mysql_fetch_assoc崩溃了php代码[关闭]

I need to change of not my PHP code and I ran into some problem (please notice that I usually code in Java and haven't develop in PHP for quite few years).

I have the following peace of code:

   $sql = "SELECT id_class FROM  events e join event_to_class ec on e.id = ec.id_event WHERE e.id = ".$r['id']."  and ec.id_evaluator = 8 and (".$stepNumbers.")";
   $mirekClassesForEvent = mysql_query($sql) or trigger_error(mysql_error().$sql); 
   if (!$mirekClassesForEvent) {
      echo "Could not successfully run query from DB: " . mysql_error();
      exit;
   }               
   $mirekClasses = array();        
   if (mysql_num_rows($mirekClassesForEvent) > 0 ) {
      while($row = mysql_fetch_assoc($mirekClassesForEvent) {
           array_push($mirekClasses, $row['id_class'];
      }
   } 

the select statement looks like this:

SELECT id_class FROM events e join event_to_class ec on e.id = ec.id_event WHERE e.id = 1 and ec.id_evaluator = 8 and (e.id_step=4 OR e.id_step=5 OR e.id_step=6)

when I run it in the MySQL DB to runs fine and returns one row of data with

id_class=27

.

However when I run the code above my php script crashes (it doesn't even show any error messages - even when I try to use mysql_error() function)

From my debuggin it looks like the problem is in the while statement, but I don't understand what could be the problem.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongshuo1856 dongshuo1856 2012-10-05 15:41

    You are missing a ) at the end of the while line, and another on the array_push line. If you are seeing no error, you probably have error reporting off. Using an editor with syntax highlighting would help you find this kind of error, as they usually include highlighting of mismatched parentheses.

    Note that you could simplify your query a little by using IN:

    ... and e.id_step IN (4,5,6)
    

    Or BETWEEN:

    ... and e.id_step BETWEEN 4 AND 6
    
    点赞 评论 复制链接分享
  • dongyue6199 dongyue6199 2012-10-05 15:41

    There's a syntax error in your code:

    while($row = mysql_fetch_assoc($mirekClassesForEvent) {
           array_push($mirekClasses, $row['id_class'];
      }
    

    You're missing the a closing bracket on your while loop:

    while($row = mysql_fetch_assoc($mirekClassesForEvent)) {
    

    Editted to add:

    And also one on the array_push, too - well spotted Kolink

    点赞 评论 复制链接分享

相关推荐