PHP交互数据库
Posted kekeoutlook
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP交互数据库相关的知识,希望对你有一定的参考价值。
图形化界面访问自己的服务器上数据库
http://ip/phpmyadmin
php文件
运行
<?php $servername = "localhost"; $username = "root"; $password = "dongdong"; $dbname = "myDB"; // 1创建数据库 // $conn = new mysqli($servername, $username, $password, $dbname); // 2检测连接 if ($conn->connect_error) { die("数据库连接失败: " . $conn->connect_error." "); } echo "连接成功<br>"; // 3使用 sql 创建数据表 //$sql = "CREATE TABLE Wechart ( // id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, // open_id VARCHAR(30) , // driver_type VARCHAR(30) , // driver_id VARCHAR(40) , // driver_content VARCHAR(50), // reg_date TIMESTAMP // )"; // if ($conn->query($sql) === TRUE) { // echo "数据表创建成功 "; // } else { // echo "Error creating database: " . $conn->error." "; // } // 4 插入数据 // $sql = "INSERT INTO Wechart (open_id, driver_type, driver_id) // VALUES (‘dongdong‘, ‘LED‘, ‘led001‘);"; //4-1插入一条消息 //if ($conn->query($sql) === TRUE) { // echo "新记录插入成功 "; //} else { // echo "Error: " . $sql . "<br>" . $conn->error; //} //4-2插入多条数据 // $sql .= "INSERT INTO Wechart (open_id, driver_type, driver_id) // VALUES (‘xixi‘, ‘LED‘, ‘led002‘);"; // $sql .= "INSERT INTO Wechart (open_id, driver_type, driver_id) // VALUES (‘nanan‘, ‘LED‘, ‘led003‘);"; // if ($conn->multi_query($sql) === TRUE) { // echo "新记录插入成功 "; // } else { // echo "Error: " . $sql . "<br>" . $conn->error; // } //5更新结果 //省去 WHERE 子句,全部操作 echo "更新数据内容<br>"; $sql = "UPDATE Wechart SET driver_content=‘36‘,open_id=‘qq‘ WHERE driver_id=‘led001‘ AND driver_type=‘LED‘"; $result = $conn->query($sql); //6 删除结果 //省去 WHERE 子句,全部操作 echo "删除数据内容<br>"; $sql = "DELETE FROM Wechart WHERE open_id=‘xixi‘"; $result = $conn->query($sql); //7单个查询结果 $sql1 = "SELECT * FROM Wechart WHERE open_id=‘dongdong‘"; $result = $conn->query($sql1); echo "找到匹配数据如下:".$result->num_rows."<br>"; while($row = mysqli_fetch_array($result)) { echo $row[‘driver_type‘] . " - " . $row[‘driver_id‘]; echo "<br>"; } // 8 输出结果 $sql1 = "SELECT open_id, driver_type, driver_id FROM Wechart"; $result = $conn->query($sql1); echo "总共数据如下:".$result->num_rows."<br>"; if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - driver_type: " . $row["driver_type"]. " -driver_id" . $row["driver_id"]. "<br>"; } } else { echo "0 无结果"; } // 关闭连接 $conn->close(); echo "数据库关闭 "; ?>
以上是关于PHP交互数据库的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 xcode 将快照划分为多个片段,以便让用户与每个片段进行交互?