duanmeng1862 2015-08-31 14:56
浏览 33
已采纳

PHP:如何将3D数组转换为mysql结果对象?

The answer might be write two functions! However, maybe a little knowledge that I do not have will help avoid a heck of a lot of re-factoring or database usage.

I have a function with the very common syntax of:

while($row = $db->sql_fetchrow($result)) {
// do some stuff
}

Most of the time, the $row will be a mysql object with zero, one or more rows therein. Simple.

However, it would be very handy if I could also utilise a three dimensional PHP array (previously built to be a bunch of "rows" with three key & value pairs) without handling them differently.

The question I have is "What do I need to do to a 3D array to "convert" it to a mysql object that will work without change in the above example"?

  • 写回答

1条回答 默认 最新

  • duanaoou4105 2015-09-07 05:30
    关注

    There isn't a way to convert into a mysql object without writing to a tmp table and reading it back in. The following will deal with either sql statement or array turning up which then negates the need to convert as the question asked.

    $all_rows = $notify->notification_sql(); // This either returns an SQL statement ready to run OR a set of rows in an array
    
    if (!is_array($all_rows)) { // If it's not an array, run the SQL query and put it into an array of rows
        $result = $db->sql_query($all_rows);
        $all_rows = array();
        while($row = mysql_fetch_array($result)) {
            $all_rows[] = $row;
        }
    }
    
    foreach...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。