Aisha Badar
Aisha Badar

Reputation: 179

Trying to get property of non-object and undefined index erroe

I just migrated a site based on custom CMS to new server. After migrating I am getting notices and warnings all over the admin area where content can be edited.

Notice: Trying to get property of non-object in database.php on line 69
Notice: Undefined index: in database.php on line 69

Following are the functions which are being used in the process:

function dbQuerySafener($query,$params=false) {
    $link= db_connect();
    if ($params) {
       foreach ($params as &$v) {
          $v = mysqli_real_escape_string($link,$v);
       }

       $sql_query = vsprintf( str_replace("?","'%s'",$query), $params );
    } else {
    $sql_query = $query;
 }
 return $sql_query;
}

// query returning ARRAY (many rows)
 function dbArray($query,$params=false) {
   $link= db_connect();
   $table_result=array();
   $r=0;
   $sql = dbQuerySafener($query, $params);
   $result = mysqli_query($link,$sql) or die(mysqli_error($link)." 
   <br>".$sql);
   while($row = mysqli_fetch_assoc($result)) {
       $arr_row=array();
       $c=0;
       while ($c < mysqli_num_fields($result)) {
          $col = mysqli_fetch_field($result);
          $arr_row[$col -> name] = $row[$col -> name]; // line 69
          $c++;
       }
    $table_result[$r] = $arr_row;
    $r++;
    }
    return $table_result;
  }

It is working as follows: A sql query will be passed to dbArray function, for example:

 $sql = "SELECT `id`, `emailAddress`, `name` FROM `gcms_users` 
 WHERE `active`=?";
 $data = dbArray($sql, array(1));

EXPECTED RESULT: The above two functions will fetch the results from database which should contains field name and its value, example:

  Array ( [0] => Array ( [id] => 1 [emailAddress] => [email protected] 
  [name] => John ) [1] => Array ( [id] => 1 [emailAddress] => 
  [email protected] [name] => Paul ) ) 

ACTUAL RESULT:

  Array ( [0] => Array ( [id] => 1 [emailAddress] => [email protected] [name] => John ) [1] => Array ( [] => ) )

There is some problem with dbArray() function while fetching the rows but I am not getting that what is missing here. Your help will be highly appreciated.

Thank you.

Upvotes: 0

Views: 102

Answers (2)

guest
guest

Reputation: 26

Pointer of mysqli_fetch_field is not reset in your first while, try

mysqli_field_seek($result, 0); after $r++;

Upvotes: 1

Tariq Rasheed
Tariq Rasheed

Reputation: 143

You cannot re-loop mysqli_num_fields(). You can fetch the columns first then loop through data.

function dbArray($query,$params=false) {
       $link= db_connect();
       $table_result=array();
       $r=0;
       $sql = dbQuerySafener($query, $params);
       $result = mysqli_query($link,$sql) or die(mysqli_error($link)." 
       <br>".$sql);

       $columns = array();
       while($col = mysqli_num_fields($result)) {
        $columns[] = $col->name;
       }

       while($row = mysqli_fetch_assoc($result)) {
           $arr_row = array();
           foreach($columns as $col){
              $arr_row[$col] = $row[$col];
           }
           $table_result[$r] = $arr_row;
           $r++;
        }
        return $table_result;
      }

Upvotes: 0

Related Questions