代码拉取完成,页面将自动刷新
<?php
## 交互模式
fwrite(STDOUT, "输入模式:1.文本,2.数据库,3.数组处理.4.默认你的名字".PHP_EOL);// ask for input
$name = trim(fgets(STDIN));// get input
if(is_numeric($name)){
switch ($name) {
case '1':
fileAction();
break;
case '2':
fwrite(STDOUT, "输入模式:1.mysqli,2.redis".PHP_EOL);
if(trim(fgets(STDIN))=='1'){
dbAction();
}else{
rdbAction();
}
break;
case '3':
$a=array("a"=>"red","b"=>"green","c"=>"blue");
arrayAction($a);
break;
default:
echo $name;
break;
}
}else{
$day1 = date("Y-m-d");
$day2 = "2021-11-06";
$diff = diffBetweenTwoDays($day1, $day2);
echo $diff."\n";
fwrite(STDOUT, "Hello, $name!");// write input back
}
function arrayAction($arr){
fwrite(STDOUT, "输入模式:pop.array_pop,shift.array_shift,slice.array_slice".PHP_EOL);
switch (trim(fgets(STDIN))) {
case 'pop':
$a = array_pop($arr);
break;
case 'shift':
$a = array_shift($arr);
break;
case 'slice':
$a = array_slice($arr);
break;
default:
// code...
break;
}
print_r ($a);print_r ($arr);
}
## 文本处理
function fileAction(){
$dir = './tmp/'.date('YmdH');
if(file_exists($dir) && is_dir($dir)){
}else{
mkdir($dir,0777);
}
$sFile = "test.txt";
$var_content="文件内容,".date("H:i:s");
$fp= fopen($dir."/".$sFile,"w");
fwrite($fp,$var_content);
fclose($fp);
}
## 数据库处理
function dbAction(){
include './app/config/db.php';// db 配置
$config = $db['db1'];
$servername = $config['DB_HOST'];
$username = $config['DB_USER'];
$password = $config['DB_PWD'];
$conn = new mysqli($servername, $username, $password);// 创建连接
if ($conn->connect_error) {// 检测连接
die("连接失败: " . $conn->connect_error);
}
echo "连接成功".PHP_EOL;
fwrite(STDOUT, "输入模式:1.创建数据库".PHP_EOL); $step = trim(fgets(STDIN));
switch ($step) {
case '1': // CREATE DATABASE
fwrite(STDOUT, "数据库名称".PHP_EOL); $name = trim(fgets(STDIN));
$sql = "CREATE DATABASE ".$name;
if ($conn->query($sql) === TRUE) {
echo "数据库创建成功".PHP_EOL;
} else {
echo "Error creating database: " . $conn->error;
}
break;
case '2': // CREATE TABLE
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
break;
case '3': // INSERT INTO
$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]');";
$conn->query($sql);
$sql .= "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('Julie', 'Dooley', '[email protected]')";
$conn->multi_query($sql);
break;
case '4': // SELECT
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 结果";
}
break;
case '5': // WHERE ORDER BY UPDATE DELETE distinct
$arr = [
"SELECT * FROM Persons WHERE FirstName='Peter'",
"SELECT * FROM Persons ORDER BY age",
"UPDATE Persons SET Age=36 WHERE FirstName='Peter' AND LastName='Griffin'",
"DELETE FROM Persons WHERE LastName='Griffin'",
"SELECT * from runoob_tbl WHERE runoob_author LIKE '%COM';",
];
$arr2 = [
"SELECT country FROM Websites UNION SELECT country FROM apps ORDER BY country;",
"SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;",
"SELECT name, COUNT(*) FROM employee_tbl GROUP BY name;",
"SELECT name, SUM(signin) as signin_count FROM employee_tbl GROUP BY name WITH ROLLUP;",
];
$arr3 = [
"SELECT * FROM runoob_test_tbl WHERE runoob_count IS NULL;",
"SELECT * from runoob_test_tbl WHERE runoob_count IS NOT NULL;",
];
$arr4 = [
"begin; # 开始事务",
"commit; # 提交事务",
"rollback; # 回滚",
];
$arr5 = [
"ALTER TABLE testalter_tbl DROP i;",
"ALTER TABLE testalter_tbl ADD i INT;",
"ALTER TABLE testalter_tbl CHANGE i j BIGINT;",
"ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;",
"ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;",
"ALTER TABLE testalter_tbl ENGINE = MYISAM;",
"ALTER TABLE testalter_tbl RENAME TO alter_tbl;",
];
$arr6 = [
"CREATE INDEX indexName ON table_name (column_name)",
"ALTER table tableName ADD INDEX indexName(columnName)",
"DROP INDEX [indexName] ON mytable;",
"CREATE UNIQUE INDEX indexName ON mytable(username(length)) ",
"ALTER table mytable ADD UNIQUE [indexName] (username(length))",
];
break;
default:
echo $step;
break;
}
}
function rdbAction(){
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server successfully".PHP_EOL;
echo "Server is running: " . $redis->ping();//查看服务是否运行
fwrite(STDOUT, "输入redi数据类型:1.string,2.hash,3.list,4.set,5.sortedset,".PHP_EOL); $type = trim(fgets(STDIN));
switch ($type) {
case 'string': // 20 SET GET GETRANGE GETSET GETBIT INCR INCRBY DECR DECRBY APPEND
$redis->set("tutorial-name", "Redis tutorial");
echo "Stored string in redis:: " . $redis->get("tutorial-name");// 获取存储的数据并输出
break;
case 'hash': // 14 HDEL HEXISTS HGETALL HINCRBY HINCRBYFLOAT HKEYS HLEN
// code...
break;
case 'list': // 17 BLPOP BRPOP BRPOPLPUSH
//存储数据到列表中
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
$arList = $redis->lrange("tutorial-list", 0 ,5);// 获取存储的数据并输出
echo "Stored string in redis";
print_r($arList);
break;
case 'set': // 17
// code...
break;
case 'sortedset': // 20
// code...
break;
default: // EXPIRE PEXPIRE EXPIREAT TTL PTTL
// code...
break;
}
}
function mdbAction(){}
function edbAction(){}
## 网络数据处理 get post put delete
/**
* 求两个日期之间相差的天数
* (针对1970年1月1日之后,求之前可以采用泰勒公式)
* @param string $day1
* @param string $day2
* @return number
*/
function diffBetweenTwoDays ($day1, $day2)
{
$second1 = strtotime($day1);
$second2 = strtotime($day2);
if ($second1 < $second2) {
$tmp = $second2;
$second2 = $second1;
$second1 = $tmp;
}
return ($second1 - $second2) / 86400;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。