Home  >  Article  >  Backend Development  >  How to get database content in php

How to get database content in php

藏色散人
藏色散人Original
2019-10-26 10:08:424158browse

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.

How to get database content in php

Processing code such as:

<?php
//从数据库根据 id 获取颜色
function getColor($db, $id)
{
    if ($result = $db->query("SELECT * FROM color where id=&#39;" . $id . "&#39;"))
    {
        $row = $result->fetch_assoc();
        return $row[&#39;color&#39;];
    }
    return &#39;#000000&#39;;
}
 
$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,&#39;1&#39;)?>">1</td>
</tr>
<tr>
<td bgcolor="<?php echo getColor($mysqli,&#39;2&#39;)?>">2</td>
</tr>
<tr>
<td bgcolor="<?php echo getColor($mysqli,&#39;3&#39;)?>">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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn