mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源

时间:2014-07-20 19:16:03

标签: php mysql

获取错误:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource .

无法知道问题是什么,但所有代码都在循环中执行

 $token = $_GET['token'];
 $q = "SELECT * FROM createpagetemp where token = '".$token."'";
 $result = mysql_query($q) or die("Couldn't execute query: $q".mysql_error());

 if (mysql_num_rows($result)) {
     while ($r = mysql_fetch_assoc($result)) {
         $InstituteName = $r['InstituteName'];
         $InstituteType = $r['InstituteType'];
         $AffilatedBy = $r['AffilatedBy'];
         $ContactNo = $r['ContactNo'];
         $Email = $r['Email'];
         $Password = $r['Password'];
         $id = $r['id'];

         $q = "INSERT INTO pageuserid  (InstituteName, InstituteType, AffilatedBy, ContactNo, Email, Password)
               VALUES ('$InstituteName', '$InstituteType', '$AffilatedBy', '$ContactNo', '$Email', '$Password')";
         $result = mysql_query($q);
         if (!$result) {
             $message = "ERROR: Query Not Executed ".mysql_error()."\n\r";
             die($message);
         }
         $q = "DELETE FROM createpagetemp where id = '".$id."' LIMIT 1";
         $result = mysql_query($q);
         if (!$result) {
             $Message = "ERROR: Unable to Delte Temp. Record".mysql_error();
             die($Message);
         }

         echo '<h1 style="text-align:center;font-weight:normal;">Your Account Has Been Activated <br>Now You Can Login</h1>';

     }
 } else {
     echo '<h1 style="text-align:center;">Token is invalid . <br>Your account may be already activated </h1>';
 }

1 个答案:

答案 0 :(得分:2)

首先:警告 - 您的代码未受到SQL注入攻击的保护!


现在我的主要答案是:你在循环中覆盖变量$result。您需要为内部$result变量指定一个新名称,例如$innerResult

                        $token = $_GET['token'] ;
                        $q = "SELECT * FROM createpagetemp where token = '".$token."'" ;
                        $result = mysql_query($q) or die("Couldn't execute query: $q".mysql_error()) ;

                        if(mysql_num_rows($result)) 
                        {
                            while($r = mysql_fetch_assoc($result))
                            {
                               // [...] skipped stuff

                               $q = "INSERT INTO pageuserid  (InstituteName, InstituteType, AffilatedBy, ContactNo, Email, Password) VALUES ('$InstituteName', '$InstituteType', '$AffilatedBy', '$ContactNo', '$Email', '$Password')" ;
                               $innerResult = mysql_query($q) ;

                               if(!$innerResult)
                               {
                                   $message = "ERROR: Query Not Executed " . mysql_error() . "\n\r" ;
                                   die($message) ; 
                               }

                               $q = "DELETE FROM createpagetemp where id = '".$id."' LIMIT 1" ;
                               $innerResult = mysql_query($q) ;

                               if(!$innerResult)
                               {
                                   $Message =  "ERROR: Unable to Delte Temp. Record" .mysql_error() ;
                                   die($Message) ;
                               }

                               echo '<h1 style="text-align:center;font-weight:normal;">Your Account Has Been Activated <br>Now You Can Login</h1>' ;
                            }
                        }

                        else {echo '<h1 style="text-align:center;">Token is invalid . <br>Your account may be already activated </h1>' ;}