douqihou7537 2018-06-03 16:06
浏览 53
已采纳

试图获取非对象和未定义索引错误的属性

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] => admin@email.com 
  [name] => John ) [1] => Array ( [id] => 1 [emailAddress] => 
  paul@email.com [name] => Paul ) ) 

ACTUAL RESULT:

  Array ( [0] => Array ( [id] => 1 [emailAddress] => admin@email.com [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.

  • 写回答

2条回答 默认 最新

  • dtkf64283 2018-06-03 16:37
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中