(创建页面,内容为“分类:编程”) |
无编辑摘要 |
||
(未显示同一用户的1个中间版本) | |||
第1行: | 第1行: | ||
[[分类:编程]] | [[分类:编程]] | ||
[https://www.runoob.com/php/php-mysql-select.html runoob.com php教程] | |||
== 页面默认跳转 == | |||
<meta charset="UTF-8" http-equiv="refresh" content = "1; url=http://www.****/index.php"> | |||
== form提交跳转 == | |||
<form method="post" action="****.php"> | |||
<input type="text" name="title" > | |||
<input type="submit" name="post1" value="id1" class="roundZone"> | |||
<input type="submit" name="post2" value="id2" class="roundZone"> | |||
</form> | |||
服务端获取参数 | |||
echo $_POST['title']; | |||
点击那个按钮,那个按钮的value被提交 | |||
echo 'p1-'.$_POST['post1']; | |||
echo 'p2-'.$_POST['post2']; | |||
==php语法== | |||
===字符串连接(拼接)=== | |||
不使用+,使用(.) | |||
===数据库日期时间格式化=== | |||
$dt = $row["tdate"]; | |||
date("Y年m月d日 H 点 i 分 s 秒",$dt).PHP_EOL | |||
==连接mysql数据== | |||
===读取数据=== | |||
$conn = new mysqli($servername, $username, $password,"database name"); | |||
// 检测连接 | |||
if ($conn->connect_error) | |||
{ | |||
die("连接失败: " . $conn->connect_error); | |||
} | |||
echo "连接成功"; | |||
$sql = "SELECT * FROM table"; | |||
$result = $conn->query($sql); | |||
if ($result->num_rows > 0) | |||
{ | |||
while($row = $result->fetch_assoc()) { | |||
echo "id: " . $row["tid"]. " - Name: " . $row["tname"]. " " . $row["tdate"]."--".$row["tfdata"]."<br>"; | |||
} | |||
} else { | |||
echo "<br>0 结果"; | |||
} | |||
$conn->close(); | |||
===插入数据=== | |||
$sql = "INSERT INTO `testtable`( `tname`, `tdate`, `tstatic`, `tfdata`) VALUES ('php in','2022-4-10','1','4.3')"; | |||
if ($conn->query($sql) === TRUE) { | |||
echo '新记录插入成功'; | |||
} else { | |||
echo 'Error: ' . '<br>' . $conn->error; | |||
} | |||
$conn->close(); | |||
==AJAX 与 PHP== | |||
===javascript=== | |||
<script language="javascript" type="text/javascript"> | |||
function inFun() | |||
{ | |||
if (window.XMLHttpRequest) | |||
{ | |||
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码 | |||
xmlhttp=new XMLHttpRequest(); | |||
} | |||
else | |||
{ | |||
//IE6, IE5 浏览器执行的代码 | |||
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); | |||
} | |||
xmlhttp.onreadystatechange=function() | |||
{ | |||
if (xmlhttp.readyState==4 && xmlhttp.status==200) | |||
{ | |||
document.getElementById("pn").innerHTML=xmlhttp.responseText; | |||
} | |||
} | |||
xmlhttp.open("GET","jsfun.php? | |||
="+document.getElementById("textbox1").value,true); | |||
xmlhttp.send(); | |||
} | |||
</script> | |||
===html=== | |||
<input id="textbox1"> | |||
<div height="80px" id="pn"> 返回值 </div> | |||
===php=== | |||
<?php | |||
$q=$_GET["q"]; | |||
if($q=="command1") | |||
$response="aaaa"; | |||
if($q=="command2") | |||
$response="bbbb"; | |||
echo $response; | |||
?> | |||
==php 上传文件== | |||
===html code=== | |||
<form action="jsfun.php" method="post" enctype="multipart/form-data"> | |||
<label for="file">文件名:</label> | |||
<input type="file" name="file" id="file" class="button"><br> | |||
<input type="submit" name="submit" value="提交" class="button"> | |||
</form> | |||
===php code=== | |||
<?php | |||
if ($_FILES["file"]["error"] > 0) | |||
{ | |||
echo "错误:" . $_FILES["file"]["error"] . "<br>"; | |||
} | |||
else | |||
{ | |||
echo "上传文件名: " . $_FILES["file"]["name"] . "<br>"; | |||
echo "文件类型: " . $_FILES["file"]["type"] . "<br>"; | |||
echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; | |||
echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"]. " kB<br>"; | |||
if (file_exists( $_FILES["file"]["name"])) | |||
{ | |||
echo $_FILES["file"]["name"] . " 文件已经存在。 "; | |||
} | |||
else | |||
{ | |||
// 如果 upload 目录不存在该文件则将文件上传到 upload 目录下 | |||
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]); | |||
echo "文件存储在: " . "upload/" . $_FILES["file"]["name"]; | |||
} | |||
} | |||
?> | |||
===错误码 1=== | |||
超过php限制的文件大小,默认2M | |||
/etc/php/7.2/apache2/php.ini | |||
upload_max_filesize = 30M | |||
post_max_size = 30M | |||
服务重启 | |||
/etc/init.d/apache2 restart | |||
==hash 哈希值== | |||
hash(string $algo, string $data, bool $raw_output = false): string | |||
参数 algo | |||
要使用的哈希算法,例如:"md5","sha256","haval160,4" 等。 在 hash_algos() 中查看支持的算法。 | |||
data 要进行哈希运算的消息。 | |||
raw_output 设置为 true 输出原始二进制数据, 设置为 false 输出小写 16 进制字符串。 | |||
$pwd = hash('ripemd160', $_POST['password']) | |||
==mysql== | |||
===create table 自增值=== | |||
create table student( | |||
id INT primary key NOT NULL AUTO_INCREMENT, | |||
name nvarchar(10) | |||
)engine=innodb default charset=utf8 | |||
==创建soap服务== | |||
apt-get install php-soap | |||
===服务端=== | |||
<?php | |||
class soapFun{ | |||
public function GetVersion($comlist){ | |||
return 'hellow,'.$comlist; | |||
} | |||
} | |||
$soapServer1 = new SoapServer(null,array('uri'=>'唯一标识')); | |||
$soapServer1->setClass('soapFun'); | |||
$soapServer1->handle(); | |||
?> | |||
===客户端=== | |||
<?php | |||
$soapClient1 = new SoapClient(null,array( | |||
'location'=>'http://地址/文件名.php', | |||
'uri'=>'唯一标识' | |||
)); | |||
echo $soapClient1->GetVersion('参数'); | |||
?> |
2024年3月15日 (五) 12:13的最新版本
页面默认跳转
<meta charset="UTF-8" http-equiv="refresh" content = "1; url=http://www.****/index.php">
form提交跳转
<form method="post" action="****.php"> <input type="text" name="title" > <input type="submit" name="post1" value="id1" class="roundZone"> <input type="submit" name="post2" value="id2" class="roundZone"> </form>
服务端获取参数
echo $_POST['title'];
点击那个按钮,那个按钮的value被提交
echo 'p1-'.$_POST['post1']; echo 'p2-'.$_POST['post2'];
php语法
字符串连接(拼接)
不使用+,使用(.)
数据库日期时间格式化
$dt = $row["tdate"]; date("Y年m月d日 H 点 i 分 s 秒",$dt).PHP_EOL
连接mysql数据
读取数据
$conn = new mysqli($servername, $username, $password,"database name"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; $sql = "SELECT * FROM table"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "id: " . $row["tid"]. " - Name: " . $row["tname"]. " " . $row["tdate"]."--".$row["tfdata"]."
"; } } else { echo "
0 结果"; } $conn->close();
插入数据
$sql = "INSERT INTO `testtable`( `tname`, `tdate`, `tstatic`, `tfdata`) VALUES ('php in','2022-4-10','1','4.3')"; if ($conn->query($sql) === TRUE) { echo '新记录插入成功'; } else { echo 'Error: ' . '
' . $conn->error; } $conn->close();
AJAX 与 PHP
javascript
<script language="javascript" type="text/javascript"> function inFun() { if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码 xmlhttp=new XMLHttpRequest(); } else { //IE6, IE5 浏览器执行的代码 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("pn").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","jsfun.php? ="+document.getElementById("textbox1").value,true); xmlhttp.send(); } </script>
html
<input id="textbox1">
返回值
php
<?php $q=$_GET["q"]; if($q=="command1") $response="aaaa"; if($q=="command2") $response="bbbb"; echo $response; ?>
php 上传文件
html code
<form action="jsfun.php" method="post" enctype="multipart/form-data"> <label for="file">文件名:</label> <input type="file" name="file" id="file" class="button">
<input type="submit" name="submit" value="提交" class="button"> </form>
php code
<?php if ($_FILES["file"]["error"] > 0) { echo "错误:" . $_FILES["file"]["error"] . "
"; } else { echo "上传文件名: " . $_FILES["file"]["name"] . "
"; echo "文件类型: " . $_FILES["file"]["type"] . "
"; echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB
"; echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"]. " kB
"; if (file_exists( $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " 文件已经存在。 "; } else { // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下 move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]); echo "文件存储在: " . "upload/" . $_FILES["file"]["name"]; } } ?>
错误码 1
超过php限制的文件大小,默认2M
/etc/php/7.2/apache2/php.ini upload_max_filesize = 30M post_max_size = 30M
服务重启
/etc/init.d/apache2 restart
hash 哈希值
hash(string $algo, string $data, bool $raw_output = false): string 参数 algo 要使用的哈希算法,例如:"md5","sha256","haval160,4" 等。 在 hash_algos() 中查看支持的算法。 data 要进行哈希运算的消息。 raw_output 设置为 true 输出原始二进制数据, 设置为 false 输出小写 16 进制字符串。 $pwd = hash('ripemd160', $_POST['password'])
mysql
create table 自增值
create table student( id INT primary key NOT NULL AUTO_INCREMENT, name nvarchar(10) )engine=innodb default charset=utf8
创建soap服务
apt-get install php-soap
服务端
<?php class soapFun{ public function GetVersion($comlist){ return 'hellow,'.$comlist; } } $soapServer1 = new SoapServer(null,array('uri'=>'唯一标识')); $soapServer1->setClass('soapFun'); $soapServer1->handle(); ?>
客户端
<?php $soapClient1 = new SoapClient(null,array( 'location'=>'http://地址/文件名.php', 'uri'=>'唯一标识' )); echo $soapClient1->GetVersion('参数'); ?>