dtftao7249656 2014-03-28 16:53
浏览 16
已采纳

从sql语句中检索数据

I have written this code which in theory i want to loop round an array and for every value use in a select statement to retrieve the applicable information. Then map a particular value id as a key and the value from the sql statement as its associated value. Though i cant seem to figure out how to add it as a value into my array im sure im a word out.

heres my code

    /*
     * Loop through the hasNewModelIdInYear and retrieve the exterior media paths
     * with a mapped id as a key.
     */

     $mediapatharray = array();

     foreach ($hasNewModelIdInYear as $key => $value) {

     $selectMediaPathFromValue = "SELECT `name` FROM `media` WHERE `id`='".$value['img1_media_id']."'";

     $res = $mysqli->query($selectMediaPathFromValue);


        $mediapatharray[$value['model_id']] = $res;
    }

All that array returns is an array full of keys but no values.. With the variable $res do i then have to ->fetch_value? as im not sure on the syntax needed in order to access the data from the query?

regards mike

  • 写回答

3条回答 默认 最新

  • dongwuli5105 2014-03-29 17:18
    关注

    Thanks for your responses i was trying to make it more complicated than it needed to be. I done it by putting the whole media table in a multidimension array then looping through them both and comparing values and if mathcing id map the name.

    simple connection and sql query to populate the array, then map the id to a key and name as its value. heres my code.

    mediaarray is an array with the media table contents populated in it

        $mediaIdPAthFromOld = array();
    
        foreach ($hasNewModelIdInYear as $hnkey => $hsvalue) {
           foreach ($mediaarray as $mpavalue) {
              if ($hsvalue['img1_media_id'] == $mpavalue['id']) {
                  $mediaIdPAthFromOld[$hsvalue['model_id']] = $mpavalue['name'];
                  }
               }
          }
    

    though this has done what i wanted i assume there is a more effient way to do this.

    regards mike

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

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致