<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test_date";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->query("set names utf8"); //可能会导致表情声明失败哦有时间测试
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
//accomplish add delete update
function run_done($sql)
{
global $conn;
if ($conn->query($sql) === TRUE) {
return 1;
} else {
return 2;
}
$conn->close();
}
//accompish select
function run_list($sql)
{
global $conn;
$arr=array();
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
$arr[]=$row;
}
return $arr;
}
//add
$sql ="INSERT INTO date_news(news_title,news_content) VALUES ('John', 'DoeDoeDoeDoeDoeDoeDoeDoeDoe')";
for ($i = 0; $i < 10000; $i++) {
run_done($sql);
}
//delete
$sql = "delete from date_news where id=1";
echo run_done($sql);
//update
//$sql="truncate table date_news"; //初始化
$sql="update date_news set news_title='8888' where id=2";
echo run_done($sql);
// 查询
$sql="select * from date_news order by id desc limit 1";
$result=run_list($sql);
for ($i = 0; $i <count($result) ; $i++) {
echo $result[$i]["id"]."\n";
}
?>