Home > Article > Backend Development > How to get database content in php
How to get the database content with php?
For example, there are three tables named 1.2.3 in HTML, and the database has three corresponding colors. How to make the background color of each table obtain the value from the database and display it.
Processing code such as:
<?php //从数据库根据 id 获取颜色 function getColor($db, $id) { if ($result = $db->query("SELECT * FROM color where id='" . $id . "'")) { $row = $result->fetch_assoc(); return $row['color']; } return '#000000'; } $mysqli = new mysqli("localhost", "test", "test", "room"); if ($mysqli->connect_error) { printf("数据库连接错误: %s\n", mysqli_connect_error()); exit(); } ?> <table border="1" cellspacing="0"> <tr> <td bgcolor="<?php echo getColor($mysqli,'1')?>">1</td> </tr> <tr> <td bgcolor="<?php echo getColor($mysqli,'2')?>">2</td> </tr> <tr> <td bgcolor="<?php echo getColor($mysqli,'3')?>">3</td> </tr> </table> <?php $mysqli->close(); ?>
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of How to get database content in php. For more information, please follow other related articles on the PHP Chinese website!