duanmei1536 2013-12-16 02:27
浏览 18

访问mysql记录集中的特定行

I have a database table with options that looks like this

Database records

I do a SELECT * FROM options and load all records into a recordset.

How do I then access just the optionValue of optionName = SMSactive

[EDIT]

Yes, I could do SELECT * FROM options WHERE optionName ='SMSactive' - I know that.

But as I said, since I have a recordset with a bunch of rows in it (from the SELECT), I want to pull a specific row where optionName = SMSactive

[EDIT] If you are using PHP >= 5.5 you can use array_column() http://php.net/array_column

But if not, the question has already been asked and answered here PHP multidimensional array search by value

  • 写回答

1条回答 默认 最新

  • dtsi9484 2013-12-16 02:30
    关注

    To access to all records with optionName = SMSactive the SQL should be:

    SELECT * FROM `TableName` WHERE `optionName` = 'SMSactive'
    

    if your array result from the query is something like this:

    $array = array(
        0 => array(
            'optionName' => 'lala0',
            'optionDescription' => 'lala01',
            'optionValue' => 'lala03',
        ),
        1 => array(
            'optionName' => 'lala1',
            'optionDescription' => 'ala2',
            'optionValue' => 'SMSactive',
        )
    );
    

    You can grab who has 'optionValue' => 'SMSactive', by this way

    foreach ($array as $key) {
        if ($key['optionValue'] == 'SMSactive') {
            $filtered[]=$key;
        }
    }
    
    echo '<pre>';
    print_r($filtered);
    echo '</pre>';
    

    Side Note: If the array is very large you have to be careful with the memory...it's advisable just get from the db what you are using in the moment...IMO

    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法