mysql simple statement PHP operation database

Add a few data to the database in advance

Add a piece of data

<meta charset="utf-8">
<?php
// 链接数据库:需要链接的服务器,用户名和密码
mysql_connect("localhost", "root", 123456);
// 选择哪个数据库
mysql_selectdb("xiaobai");
// 设置字符集
mysql_query("SET NAMES UTF8");
// 向对应的数据库插入数据
$sql = "INSERT INTO form VALUES('小李',17,'男',1358995225,'[email protected]',88,'河北石家庄')";
//查询数据库中的数据
//$sql = "SELECT * FROM form";
//执行命令
mysql_query($sql);
?>

At this time, every time a page is requested, a piece of data will be added to the database 

The mysql_query() function executes a MySQL query

flow chart

Query data in the database

<meta charset="utf-8">
<?php
// 链接数据库:需要链接的服务器,用户名和密码
mysql_connect("localhost", "root", 123456);
// 选择哪个数据库
mysql_selectdb("xiaobai");
// 设置字符集
mysql_query("SET NAMES UTF8");
// 向对应的数据库插入数据
//$sql = "INSERT INTO form VALUES('小李',17,'男',1358995225,'[email protected]',88,'河北石家庄')";
//查询数据库中的数据
$sql = "SELECT * FROM form";
//执行命令
$result = mysql_query($sql);

echo $result;
print_r($result);
?>

At this time, the output of this $result is an error, because echo and print_r output basic type values ​​and arrays, and the return result of $result is not a basic type value, nor an array

So to convert the result to an array

The mysql_fetch_array() function gets a row from the result set as an associative array, or a numeric array, or both

$arr = mysql_fetch_array($result);
print_r($arr);
echo "<br/>";

I want to print all the contents of the array, but what is returned is a piece of data. After repeatedly executing the above command, I found

Print one item at a time. This is definitely unreasonable. The solution is to use a while loop to traverse all the data.

while($arr = mysql_fetch_array($result)) {
    print_r($arr);
    echo "<br/>";
}

The data is rendered into the table  

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    table,
    td,
    th {
        border-collapse: collapse;
        line-height: 30px;
        border: 1px solid #333;
        padding: 0 20px;
    }
</style>

<body>
    <?php
    // 链接数据库:需要链接的服务器,用户名和密码
    mysql_connect("localhost", "root", 123456);
    //选择哪个数据库
    mysql_selectdb("xiaobai");
    // 设置字符集
    mysql_query("SET NAMES UTF8");
    // 向对应的数据库插入数据
    $sql = "SELECT * FROM form";
    $result = mysql_query($sql);
    ?>
    <table>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>电话</th>
            <th>邮箱</th>
            <th>成绩</th>
            <th>地址</th>
        </tr>
        <?php
        while ($arr = mysql_fetch_array($result)) { ?>
            <tr>
                <td><?php print_r($arr['name']) ?></td>
                <td><?php print_r($arr['age']) ?></td>
                <td><?php print_r($arr['sex']) ?></td>
                <td><?php print_r($arr['tel']) ?></td>
                <td><?php print_r($arr['email']) ?></td>
                <td><?php print_r($arr['score']) ?></td>
                <td><?php print_r($arr['adress']) ?></td>
            </tr>
        <?php } ?>
    </table>
</body>

</html>

Returns the number of results found

$num = mysql_num_rows($result);

 

 

 

Guess you like

Origin blog.csdn.net/weixin_41040445/article/details/114824979