PHP mysqli 增删改查(一)

$mysql_server_name="localhost"; //数据库服务器名称
    $mysql_username="root"; // 连接数据库用户名
    $mysql_password="111111"; // 连接数据库密码
    $mysql_database="test"; // 数据库的名字
    $mysql_prot = "3306";

    $mysqli = new mysqli($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
    if($mysqli){
        echo "mysqli success"."
"
; }else{ echo "mysqli error"."
"
; } $query = "select * from user"; //查询name字段 $sql1 = "select * from user WHERE id = 0"; //插入数据 $sqlInsert = "insert into user (id,name) values('6','hah')"; //删除数据 $sqldelete = "delete from user where id = 6"; //修改数据 //$sqlUpdate = "update user set name = 'weizhi2' where name = 'iceboy'"; $sqlUpdate = "update user set id = 5 where id = 0"; // $res = $mysqli->query($sql1); // $res = $mysqli->query($sqlInsert); //$res = $mysqli->query($sqldelete); $res = $mysqli->query($sqlUpdate); echo $mysqli->affected_rows; if($res){ echo "query success"."
"
; // 三种处理查询结果 //喜欢使用后面两种方法 // 方法一 // while(list($id,$name)=$res->fetch_row()){ // echo "id: ".$id." name:".$name."
"
; // } // 方法二 // while($row = $res->fetch_object()){ // echo "id:".$row->id." name:".$row->name."
"
; // // } //方法三 // while ($row = $res->fetch_assoc()){ // echo "id:".$row["id"]." name:".$row["name"]."
"
; // } }else{ echo "query error"."
"
; } //$res->free(); $mysqli->close(); ?>

你可能感兴趣的:(PHP)