码迷,mamicode.com
首页 > 数据库 > 详细

PHP交互数据库

时间:2018-12-29 21:03:04      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:localhost   new   bubuko   mysq   mys   min   tar   query   结果   

教程

图形化界面访问自己的服务器上数据库

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."\n");
} 
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 "数据表创建成功\n";
// } else {
    // echo "Error creating database: " . $conn->error."\n";
// }

// 4 插入数据
// $sql = "INSERT INTO Wechart (open_id, driver_type, driver_id)
// VALUES (‘dongdong‘, ‘LED‘, ‘led001‘);";

//4-1插入一条消息
//if ($conn->query($sql) === TRUE) {
//	  echo "新记录插入成功\n";
//} 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 "新记录插入成功\n";
// } 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 "数据库关闭\n";

?>

  

PHP交互数据库

标签:localhost   new   bubuko   mysq   mys   min   tar   query   结果   

原文地址:https://www.cnblogs.com/kekeoutlook/p/10197685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!