Error

Hi i seem to get this error, what may be the problem?

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/socialch/public_html/external/update2.php on line 6
working

code is


<?php
include "../database.php";

$result = mysql_db_query($dbname, "SELECT * FROM engine4_users WHERE enabled = '1'  ") or die (mysql_error);
if (mysql_num_rows($result)) {
   while ($qry = mysql_fetch_array($result)) {



$request = "INSERT INTO `engine4_authorization_allow` ( `resource_type` , `resource_id` , `action` , `role` , `role_id` , `value` , `params`) 

VALUES ( 

'user', '$qry[user_id]', 'view','everyone','0','1','');";
$result = mysql_query($request) or die(mysql_error()); 



}
}

?>

Hi,
Give this a try:


<?php

include("../database.php");


    $sql = "SELECT * FROM `engine4_users` WHERE `enabled` = '1'";
    
    $result = mysql_query($sql) or die(mysql_error());
    
    $query_check = mysql_num_rows($result);

    if (!$query_check) {

        echo"Problem with query";
        
    }else{

            while ($row = mysql_fetch_array($result)) {

            extract($row);
            
            $user_id    = $row["user_id"];

            $sql2 = "INSERT INTO `engine4_authorization_allow` ( `resource_type` , `resource_id` , `action` , `role` , `role_id` , `value` , `params`)

            VALUES ('user', '$user_id', 'view','everyone','0','1','')";
            
            $result2 = mysql_query($sql2) or die(mysql_error());

             $query_check2 = mysql_num_rows($result2);

                      if (!$query_check2) {

                      echo"Problem with query2";
                      }
            
            }

    }

?>

  1. “mysql_db_query” has been deprecated as of PHP 5.3.0. Use mysql_query instead.
  2. The variable “$result” is used once as the result of the “insert” query, and then in the while it is an argument of “mysql_fetch_array”. It is impossible to fetch rows from an insert query.
  3. In addition to Kevin’s advice, I would suggest using “heredoc” for my sql queries.
    For example:
    $sql2 = <<<EOQ
    INSERT INTO engine4_authorization_allow
    ( resource_type , resource_id , action , role , role_id , value , params)
    VALUES (‘user’, ‘$user_id’, ‘view’,‘everyone’,‘0’,‘1’,‘’)
    EOQ;

“EOQ;” must be placed at the very beginning of the line.