dtoaillwk759656786 2016-03-17 17:18
浏览 26

css中mySQL的变量不起作用

I am trying to use a form to set colors in a table in my database, then call those colors into my stylesheet (style.php)

Defining the color variables manually works:

<?php 
    header("Content-type: text/css"); 
    $color1  = '#cc0000';
?>

#thisdiv { background-color: <?php echo $color1; ?>; }

But this does not:

<?php 
    header("Content-type: text/css"); 
    $getSettings = mysqli_query($db, "SELECT * FROM settings WHERE setting_id = 1");
    $setting     = mysqli_fetch_assoc($getSettings);
    $color1      = $setting['setting_color1'];
?>

#thisdiv { background-color: <?php echo $color1; ?>; }

My 'settings' table looks like this:

setting_id | setting_color1 | setting_color2 | setting_color3
1          | #cc0000        | #000000        | #ffffff

How can I get this working properly? Maybe I am doing something wrong, or maybe I am just overlooking something stupid. Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dsvbtgo639708 2016-03-17 17:30
    关注
    <?php 
        header("Content-type: text/css"); 
        $getSettings = mysqli_query($db, "SELECT * FROM settings WHERE setting_id = 1");
        // --------------------------^ and where is that defined?
        $setting     = mysqli_fetch_assoc($getSettings);
        $color1      = $setting['setting_color1'];
    ?>
    
    #thisdiv { background-color: <?php echo $color1; ?>; }
    

    You should always verify if your query ran successfully, isn't that hard.

    <?php
      if($result = mysqli_query($db, 'SELECT * FROM table') != false){
        var_dump(mysqli_fetch_assoc($result));
        // hurray, query ran successfully.
        // Now execute the code that sets the variable.
      } else {
        // On fail, perhaps load from cache?
      }
    ?>
    

    Secondly, your DB structure is wrong in my opinion. Try something like the following.

    settings:

    id (primary key)| key (varchar)| value (varchar)| type (varchar)| decryption (text) | theme (int)
    1                 mycolor        AABBCC           hex             Background of we..   1
    
    $sql = 'SELECT key, value, type FROM settings WHERE theme = 1'; // etc..
    

    This way, you have much more control over your 'theme'. Then you can while-fetch the results and set: $arr[$row['key']] = $row['value'].

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真