mysqli连接数据库PhpStorm配置

PHP mysqli笔记

PHP连接数据库有面向对象风格和过程化风格


创建连接

面向对象风格

$mysqli = new mysqli("localhost", "my_user", "my_password", "DataBase","port");

//localhost:    连接地址,数据库在本地电脑就用localhost或者127.0.0.1
//my_user:      账号
//my_password:  密码
//DataBase:     数据库(可有可无)
//port:         端口号(也可以在连接地址处添加,例:localhost:8888)


过程化风格

link = mysqli_connect("localhost", "my_user", "my_password", "world");


简单的查询

面向对象

$result = $mysqli->query("select count(*) from first.city");

过程化风格

$result = mysqli_query($link,"select count(*) from first.city");
//参数:(mysql $link,SQL语句)


将查询到信息展示到页面上

方法很多

<a href="Add.php">添加a>
<table border="1" align="center">
    <tr>
        <td>IDtd>
        <td>城市名称td>
        <td>地区坐标td>
        <td>操作td>
    tr>



//拼接方法一:
while($row=$result->fetch_assoc()){ //获取一行
    echo '';
    echo ''.$row['Id'].'';
    echo ''.$row['CityName'].'';
    echo ''.$row['CityLocation'].'';
    echo '$row['Id'].'\'">';
    echo '$row['Id'].'\'">';
    echo '';
}

//拼接方法二:
while($row=$result->fetch_assoc()){ //获取一行
?>
    <tr>
       <td> echo $row['Id']?>td>
       <td> echo $row['CityName']?>td>
       <td> echo $row['CityLocation']?>td>
       <td><input type="button" value="修改" onclick="location.href='modify.php?Id='">td>
       <td><input type="button" value="删除" onclick="location.href='delete.php?Id='">td>
    tr>

}

$result->free();//释放
$mysqli->close();//释放
?>

table>


简单的删除

通过Get获取传递过来的Id
在delete.php页面中执行删除SQL语句

$id = $_GET['Id']??-1;//获取Id的值,如果没有就赋值为-1

$sql = "delete from first.city where Id = $id";//拼接SQL语句

if($mysqli->query($sql))//执行成功返回true
    echo "删除成功";
else
    echo "删除失败";


简单的修改

通过Get获取传递过来的Id
modify.php页面

//GET

//获取Id值
$id = $_GET['Id']??-1;//获取Id的值,如果没有就赋值为-1

//此处省去创建mysqli对象等重复代码

$sql = "select * from first.city where id = $id";//拼接SQL语句
$row = $mysqli->query($sql)->fetch_assoc();//获取第一行

<form action="" method="Post" id="form1">
    <table>
        <tr><td colspan="2">修改商品td>tr>
        <tr>
            <td>城市名称td>
            <td><input type="text" name="CityName" value="">td>
        tr>
        <tr>
            <td>地区坐标td>
            <td><input type="text" name="CityLocation"  value="">td>
        tr>
        <tr>
            <td><input type="submit" value="保存">td>
        tr>
    table>
form>
//POST

//获取POST请求数据
if(isset($_POST['CityName']) && isset($_POST['CityLocation'])) {

    $name = $_POST['CityName'];//获取城市名称
    $location = $_POST['CityLocation'];//获取城市坐标

    $sql = "update first.city set CityName = '$name',CityLocation = '$location' where Id = $id";//拼接update SQL

    if ($mysqli->query($sql)) {//执行更新语句,执行成功返回True
        echo "修改成功";
        header('location:Index.php');//跳转页面
    } else {
        echo "修改失败";
    };

}

简单的添加

添加,Add.php页面

//获取POST请求的数据
if(isset($_POST['CityName']) && isset($_POST['CityLocation'])) {

    $name = $_POST['CityName'];//获取城市名称
    $location = $_POST['CityLocation'];//获取城市坐标

    $sql = "insert into first.city(CityName,CityLocation) values('$name','$location')";//拼接添加SQL语句

    if ($mysqli->query($sql)) {//执行SQL语句
        echo "添加成功";
        header('location:Index.php');
    } else {
        echo "添加失败";
    };
<form action="" method="Post">
<table>
    <tr>
        <td>城市名称td>
        <td><input type="text" name="CityName">td>
    tr>
    <tr>
        <td>地区坐标td>
        <td><input type="text" name="CityLocation">td>
    tr>
    <tr>
        <td><input type="submit" value="添加">td>
    tr>
table>
form>

PhpStorm

使用PhpStorm的一些配置

PHP配置

Setting->Languages&Frameworks->PHP

mysqli连接数据库PhpStorm配置_第1张图片


Project Configuration->CLI Interpreter

mysqli连接数据库PhpStorm配置_第2张图片


左上角添加->Other Local…

mysqli连接数据库PhpStorm配置_第3张图片


PHP executable为PHP安装的路径

mysqli连接数据库PhpStorm配置_第4张图片


添加完成后,设置后就行

mysqli连接数据库PhpStorm配置_第5张图片



Apache配置

PhpStrom配置Apache服务器

Setting -> Build,Execution,Deployment ->Deployment

mysqli连接数据库PhpStorm配置_第6张图片


左上角添加

mysqli连接数据库PhpStorm配置_第7张图片


Type 看情况而定,如果是在本地就选Local or mounted folder

mysqli连接数据库PhpStorm配置_第8张图片


Folder : Apache配置的虚拟路径
Web Server or URL : Apache配置的虚拟主机路径(看情况而定)
mysqli连接数据库PhpStorm配置_第9张图片

你可能感兴趣的:(PHP)