我的世界我爱编程

PHP数据库MySQL部分

2018-09-20  本文已影响0人  九尾74

1.环境部署

   XXamp是真的强,简化了mysql后,继承到这里,虽然,往后发展局限性很强不过这完全无所谓,

本来就是主打的小中型项目。

    所以如果说是环境部署,如果配置了PHP的基本上都已经搭建好了!

2.数据库起手

1.链接数据库的语句

2.需要执行的语句

3.两者之间的联系

4.关闭数据库


/**

* Created by PhpStorm.

* User: Administrator

* Date: 2018/9/20 0020

* Time: 18:15

*/

$servername = "localhost";

$username = "root";

$password = "";

/*-----------------mysqli创建数据库---------------------*/

//////创建链接

//$conn=new mysqli($servername,$username,$password);

//

/////测试链接

//

//if ($conn->connect_error)

//{

//    die("链接失败:".$conn->connect_error);

//}

//echo "链接成功".PHP_EOL;

//

/////创建数据库

//

//$sql="CREATE DATABASE myDB";

//

//if (mysqli_query($conn,$sql))

//{

//    echo "创建成功";

//}

//else {

//

//      echo "请重试".mysqli_connect_error();

//    }

//mysqli_close($conn);

/*----------------------pdo来创建数据库------------------------*/

try {

///创建链接

    $conn = new PDO("mysql:host=$servername", $username, $password);

///数据库语句

    $sql = "CREATE DATABASE thebase";

///执行数据库语句

    $conn->exec($sql);

    echo "再次创建成功";

}

catch (PDOException $e)

{

  echo  "创建失败".$e->getMessage();

}

?>

以上便是,从链接数据库到创建数据库的过程,两个办法,或一个办法的,如果程序出错了的话

可以比较清楚的知道出错部分在哪里!


2.数据库的增删改查


/**

* Created by PhpStorm.

* User: lck

* Date: 2018/9/22 0022

* Time: 11:28

*/

$servername="localhost";

$usename="root";

$password="";

$dbname="myDB";

////创建链接

$conn=new mysqli("$servername","$usename","$password",$dbname);

if ($conn->connect_error)

{

    die("链接失败".$conn->connect_error);

}

echo "链接成功".PHP_EOL;

///创建表

///

//$thesql = "CREATE TABLE thesql (

//id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

//firstname VARCHAR(30) NOT NULL,

//lastname VARCHAR(30) NOT NULL,

//email VARCHAR(50),

//reg_date TIMESTAMP

//)";

//

//if ($conn->query($thesql)==TRUE)

//{

//    echo "创表成功!";

//}

//else

//    {

//        echo "创表失败,请重试";

//    }

///插入数据

///

//$insetsql = "INSERT INTO MyGuests (firstname, lastname, email)

//VALUES ('lck', 'lck1', 'lck@example.com')";

//

//if ($conn->query($insetsql)==TRUE)

//{

//    echo "插入成功!!";

//}

//else

//{

//    echo "插入失败".$conn->connect_error;

//}

///插入多条数据

//$sql = "INSERT INTO MyGuests (firstname, lastname, email)

//VALUES ('John', 'Doe', 'john@example.com');";

//$sql .= "INSERT INTO MyGuests (firstname, lastname, email)

//VALUES ('Mary', 'Moe', 'mary@example.com');";

//$sql .= "INSERT INTO MyGuests (firstname, lastname, email)

//VALUES ('Julie', 'Dooley', 'julie@example.com')";

//

//mysqli_multi_query($conn, $sql) 可用这条

//if ($conn->multi_query($sql)==TRUE)

//{

//    echo "多条数据插入成功";

//}

//else

//    {

//        echo "多条数据插入失败";

//    }

///预处理语句

///

///i - integer(整型)

//d - double(双精度浮点型)

//s - string(字符串)

//b - BLOB(binary large object:二进制大对象)

///

///

//$stmt=$conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");

//$stmt->bind_param("sss",$firstname,$lastname, $email);

//

//// 设置参数并执行

//$firstname = "John";

//$lastname = "Doe";

//$email = "john@example.com";

//$stmt->execute();

//

//$firstname = "Mary";

//$lastname = "Moe";

//$email = "mary@example.com";

//$stmt->execute();

//

//$firstname = "Julie";

//$lastname = "Dooley";

//$email = "julie@example.com";

//$stmt->execute();

//

//echo "数据插入成功";

//

//$conn->close();

//$stmt->close();

////查询数据

//$sql = "SELECT id, firstname, lastname FROM MyGuests";

//$reuslt=$conn->query($sql);

//

//if($reuslt->num_rows>0)

//{

//

//    ///遍历数据

//      while($row=$reuslt->fetch_assoc())

//    {

//        echo "id为:".$row["id"]."firstname为:".$row["firstname"]."lastname为:".$row["lastname"];

//    }

//}

///按条件进行查询

//$reuslt=mysqli_query($conn,"SELECT * FROM MyGuests

//WHERE firstname='lck'");

//

//while ($row=mysqli_fetch_array($reuslt))

//{

//    echo "id为:".$row["id"]."firstname为:".$row["firstname"]."lastname为:".$row["lastname"];

//}

//

////数据排序

//$reuslt=mysqli_query($conn,"SELECT * FROM MyGuests

// ORDER BY id ");

//

//while ($row=mysqli_fetch_array($reuslt))

//{

//    echo "id为:".$row["id"]."firstname为:".$row["firstname"]."lastname为:".$row["lastname"];

//}

////数据更新

//$reuslt=mysqli_query($conn,"UPDATE MyGuests SET LastName='lck3'

//WHERE firstname='lck' AND LastName='lck1'");

////数据删除

//mysqli_query($conn,"DELETE FROM MyGuests WHERE lastname='lck3'");

mysqli_close($conn);

?>

部分效果图:



2-1


2-2
上一篇下一篇

猜你喜欢

热点阅读