简体   繁体   中英

how can i get an image from database through id with css?

I have this database table:

ID Nome Country Imagem
1  John USA     images/######.jpg
2  Ana  USA     images/######.jpg
3  ##   ##      images/######.jp

How can I start my code? I Want to get the image through the ID and put it in the background.

.background {
   backgound-image: url(...);
}

Is This Possible?

I think it's possible when you link php file as css.

And return appropriate header

Like here: https://stackoverflow.com/a/2906870/1011278

2) Second variant is to do something like this:

.background {
   backgound-image: url(/user-image.php?id=1);
}

If i got right.

You can write your css code in your header.php or footer.php like,

<?php
$imgUrl = 'images/######.jpg';  //get corresponding filename and path from db
?>
<style>
.background {
   backgound-image: url(<?php echo $imgUrl ?>);
}
</style>
<?php
?>

Do you know MySQL?

Anyway, after the connection to the DB, do this:

$bg = $mysqli->query("SELECT Imagem FROM YOURTABLE WHERE ID=2")->fetch_object()->Imagem;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM