php笔记04

限于喜欢 提交于 2020-02-28 13:43:36

PHP+MySQL

连接数据库 mysqli

<?php
        $servername="localhost";
        $username="root";
        $password="******";
        $link=new mysqli($servername,$username,$password);
        if($link->connect_error){
            echo "连接失败";
        }
        echo "连接成功";
        ?>

创建表

<?php
        $servername="localhost";
        $username="root";
        $password="******";
        $dbname="user";
        $link=new mysqli($servername,$username,$password,$dbname);
        if($link){
            echo "连接成功!<br>";
        }
        $create="create table user(
            usename INT(10) PRIMARY KEY NOT NULL,
            passwd VARCHAR(30) NOT NULL
        )";
        if($link->query($create)){
            echo "表创建成功!<br>";
        }else{
            echo "表创建失败!<br>";
        }
        $link->close();
        ?>

插入数据

<?php
        $servername="localhost";
        $username="root";
        $password="******";
        $dbname="user";
        $link=new mysqli($servername,$username,$password,$dbname);
        if($link){
            echo "连接成功!<br>";
        }
    $usenamex=23324443;
    $passwdx="wfwej.of";
        $sql="insert into user(usename,passwd) values($usenamex,'$passwdx')";
        if($link->query($sql)){
            echo "成功!<br>";
        }else{
            echo "失败!<br>";
        }
        $link->close();
        ?>

查询数据

<?php
        $servername="localhost";
        $username="root";
        $password="******";
        $dbname="user";
        $link=new mysqli($servername,$username,$password,$dbname);
        if($link){
            echo "连接成功!<br>";
        }
    $usenamex=23343443;
    $passwdx="wfwej.of";
        $sql=$link->query("select * from user");
         foreach($sql as $a){
             echo "id:",$a['id'],"<br>user:",$a['usename'],"<br>passwd:",$a['passwd'];
             echo "<br><br>";
         }      
        $link->close(); 
        ?>

以此类推。。。。。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!