Reputation: 756
So i read article on css-tricks.com˙(http://css-tricks.com/css-variables-with-php/) about PHP in CSS and I tried it myself. I'm having problem structuring it. To be specific, i'm using Sql to take image from database and use result as a background image. Like this:
<?php
header("Content-type: text/css; charset: UTF-8");
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("login", $con);
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result))
{
?>
Then goes my css, i will just write needed ID:
#user {
background: url(../img/<?php echo $row['cover']; ?>) fixed;
}
And at the end, I close connection:
<?php
};
mysql_close($con);
?>
Everything works except css is outputted double, that is listed twice.Can anyone point the problem? Thanks.
Upvotes: 0
Views: 106
Reputation: 1391
It will return as many values as you have in your DB table "users"
If you only want 1 result you need to write
"SELECT * FROM users LIMIT 1"
You dont have a "WHERE" statement for the query?
"SELECT * FROM users WHERE SOME_COLUMN = $your_variable "
Upvotes: 0
Reputation: 9583
you're going to create:
background: url(../img/<?php echo $row['cover']; ?>) fixed;
for each row returned by the database. you need to limit the results, either in the sql like:
$result = mysql_query("SELECT * FROM users where userId = $userId");
or in the php like:
if ($row['userId']==$userId){ ?>
background: url(../img/<?php echo $row['cover']; ?>) fixed;
<?php}
Upvotes: 1