Question

I'm trying to make a CSS file which alters a color based on the user that is logged in however I can't seem to run a SQL query from inside the CSS document. Doing so negates the css...

At the top of the main page I have:

<link rel="stylesheet" href="css/dynamictag.php" media="screen">

And the CSS works when the file looks like the following:

<?php
    header("Content-type: text/css; charset: UTF-8");
?>
#colortag {
    width: 30px;
    height: 50px;
    float: left;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    -webkit-box-shadow: 1px 0px 5px;
    box-shadow: 1px 0px 5px;
    margin-left: 5px;
    margin-top: -5px;
    background-color: #FFFFFF;
    margin-right: 5px;
}
#theuser {
    float: left;
    font-size: small;
}

But it doesn't apply whatsoever when I have the following:

<?php
    header("Content-type: text/css; charset: UTF-8");
?>
<?php
$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session';"
        $result=mysql_query($query);
        $row=mysql_fetch_array($result);
        $color='#'.$row['Color'];
?>
#colortag {
    width: 30px;
    height: 50px;
    float: left;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    -webkit-box-shadow: 1px 0px 5px;
    box-shadow: 1px 0px 5px;
    margin-left: 5px;
    margin-top: -5px;
    background-color: <?php echo $color;?>;
    margin-right: 5px;
}
#theuser {
    float: left;
    font-size: small;
}

Is there another means of doing this? Am I doing something wrong?

Thanks

Was it helpful?

Solution

The following line is missing a semicolon at the end (and instead has it inside the SQL statement), so the PHP is not executing correctly.

$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session';"

It should be:

$query="SELECT Color FROM UserColors JOIN Users ON UserColors.idUserColors=Users.UserColors_idUserColors WHERE user_name='$login_session'";

Also, you should be using mysqli or PDO. Using mysql_query in PHP is deprecated, as seen on the mysql_query PHP page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top