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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧