PHP--資料庫基本操作

BtWangZhi發表於2017-10-19

1 增刪改查

<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php 
    $servername="127.0.0.1";
    $username="root";
    $password="123123";
    $dbname="test";

    //建立連線
    $conn=new mysqli($servername,$username,$password,$dbname);
    if($conn->connect_error){
        die("連線失敗".$conn->connect_error);
    }
    echo "連線成功";

    //新增
    /* $sqladd="INSERT INTO user(name,password) VALUES('123','123')";
    //執行新增
    mysqli_query($conn, $sqladd); */

    //刪除
    /* $sqldelete="delete from user where id=1";
     mysqli_query($conn, $sqldelete);
     echo "刪除成功"; */

    //更新
    $sqlUpdate="update user set name=111 where password='123'";
    mysqli_query($conn, $sqlUpdate);
    echo "<br>更新成功";

    //查詢
    /* $sql="select * from user";
    $result=$conn->query($sql);
    foreach ($result as $row){
        echo "id:".$row["id"]."name".$row["name"]."password".$row["password"]."<br>";
    } 
    echo "查詢成功";
    */
    mysqli_close($conn);
?>
</body>
</html>

相關文章