修改显示的根目录
enabled是启动文件,里面默认放的是availble文件夹中的配置文件的软链接。avaibled中的放的文件才是真正的配置文件。
//etc/apache2/apache2.conf
<Directory /home/zz/www>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
//000-default.conf
ServerAdmin webmaster@localhost
DocumentRoot /home/zz/www/html
显示中文乱码问题
<html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网页测试</title>
<body>
<p>这是一个测试文件</p>
</body>
</html>
//在网页上加上才行
php基本语法
基本数据类型:integer,float,string,boolean,array,object
注意:变量的类型是由赋给的值决定的
echo "hello the word";
echo date("Y-d-d h:i:s");
$val=312;
$f==(float)val;//强制转换
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
//welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
操作符
算数操作
+ - * / %
字符串操作符
<?php
$a='欢迎';
$b='光临';
$c=$a.$b
?>
赋值= ,
比较>,<,!=,>=,<=,<>
逻辑|| ,&& ,!
5>7?“yes”:“no”
控制结构
和c++一样
++
if()
esle if()
else
define('ZYLG',666)
数组和正则表达式
同一个数组可以有多个数据类型,同时可以关联数据
$student_info=array(‘name’=>'zylg','class'=>'15003301')
int ereg ereg(".*@qq\.com","zylgcmd@qq.com")//true
string ereg_replace('@','\$','zylgcmd@qq.com')
$add=split('\.|@','zylgcmd@qq.com')//zylgcmd qq com
函数及代码的服用``
<html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网页测试</title>
<body>
<?php
$val=1;
printcount($val);
function printcount($num)
{
while($num<100){
echo "now number is $num<br> ";
$num++;
}
}
?>
<p>这是一个测试文件</p>
</body>
</html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网页测试</title>
<body>
<?php
$val=1;
$sum=sumFunction($val);
function sumFunction($num)
{
$sum=0;
while($num<=100){
$sum+=$num;
$num++;
}
return $sum;
}
echo "$sum"
?>
<p>这是一个测试文件</p>
</body>
</html>
<?php
$sum=0;
sumFunction($sum);
function sumFunction(&$num)
{
for($i=1;$i<=100;$i++)
{
$num+=$i;
}
}
echo "$sum"
?>
include
//myfunction.php
<html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<?php
$sum=0;
sumFunction($sum);
function sumFunction(&$num)
{
for($i=1;$i<=100;$i++)
{
$num+=$i;
}
}
echo "$sum"
?>
</body>
</html>
<html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网页测试</title>
<body>
<?php
include("myfunction.php")
?>
</body>
</html>
数据库的使用
<?php
$host = 'localhost';
$database = 'test';
$username = 'root';
$password = 'root';
$selectName = 'harry';//要查找的用户名,一般是用户输入的信息
$insertName = 'testname';
// 创建对象并打开连接,最后一个参数是选择的数据库名称
$mysqli = new mysqli($host, $username, $password, $database);
// 编码转化为 utf8
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
if (mysqli_connect_errno()) {
// 诊断连接错误
die("could not connect to the database.\n" . mysqli_connect_error());
}
$selectedDb = $mysqli->select_db($database);//选择数据库
if (!$selectedDb) {
die("could not to the database\n" . mysql_error());
}
if ($stmt = $mysqli->prepare("select * from user where name = ?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $selectName);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($name, $age);
/* fetch values */
while ($stmt->fetch()) {
echo "Name: $name Age: $age \n";
}
/* close statement */
$stmt->close();
}
//添加记录
if ($insertStmt = $mysqli->prepare("insert into user(name, age) values(?, 18)")) {
/* bind parameters for markers */
$insertStmt->bind_param("s", $insertName);
/* execute query */
$insertStmt->execute();
echo $insertStmt->affected_rows . "\n";
/* close statement */
$insertStmt->close();
}
//更新记录
if ($updateStmt = $mysqli->prepare("update user set age = 19 where name=?")) {
/* bind parameters for markers */
$updateStmt->bind_param("s", $insertName);
/* execute query */
$updateStmt->execute();
echo $updateStmt->affected_rows . "\n";
/* close statement */
$updateStmt->close();
}
//删除记录
$result = $mysqli->query("delete from user where age = 19");
echo $result . "\n";
$mysqli->close();//关闭连接
/* Connect to a MySQL server 连接数据库服务器 */
$con = mysqli_connect(
'localhost', /* The host to connect to 连接MySQL地址 */
'zz', /* The user to connect as 连接MySQL用户名 */
'123', /* The password to use 连接MySQL密码 */
'mydata'); /* The default database to query 连接数据库名称*/
if (!$con) {
printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
exit;
}else
echo '数据库连接上了!';
mysqli_set_charset($con,'utf8');
if ($result = mysqli_query($con, 'SELECT sno,class,sname FROM students ')) {
echo "<br>". '学号 班级 姓名 '. "<br/>";
/* Fetch the results of the query 返回查询的结果 */
while( $row = mysqli_fetch_assoc($result) ){
echo $row['sno'], " ", $row['class'], " ", $row['sname'], "<br/>";
// printf("%s (%s) ", $row['id'],$row['class'], $row['scores']);
}
/* Destroy the result set and free the memory used for it 结束查询释放内存 */
mysqli_free_result($result);
}
else
{
printf("Can't get result %s ", mysqli_error());
}
/* Close the connection 关闭连接*/
mysqli_close($con);
?>
if ($result1 = mysqli_query($con,"INSERT INTO students(sno,sname,ssex,class)VALUES('150','开心','男','15')"))
{
echo "insert into successed";
mysqli_free_result($result1);
}
else
{
echo "insert fail";
}
//===============================数据的删除===================================
if ($result1 = mysqli_query($con,"delete from students where sname='开心'"))
{
echo "delete successed<br>";
mysqli_free_result($result1);
}
else
{
echo "delete fail<br>";
}
<?php
//==================================连接数据库================================
$con = mysqli_connect(
'localhost', /* The host to connect to 连接MySQL地址 */
'zz', /* The user to connect as 连接MySQL用户名 */
'123', /* The password to use 连接MySQL密码 */
'mydata'); /* The default database to query 连接数据库名称*/
if (!$con) {
// printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
exit;
}else
// echo '数据库连接上了!';
//==================================数据库插入=================================
$sname=$_POST["name"];
$sno=$_POST["no"];
$sql="insert into sstudent(sno,sname)values('".$sno."','".$sname."')";
//echo $sql;
if ($result= mysqli_query($con,$sql))
{
echo "$sno Succes";
mysqli_free_result($result);
}
else
{
echo "<script>alert('data fail')</script>";
echo "$sno Fail";
}
mysqli_close($con);
<html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网页测试</title>
<body>
<p align="center">欢迎你的到来</p>
<form align="center" action="connectMysql2.php" method="post" onsubmit="sNameCheck()" >
学号 <input type="text" placeholder="学号" name="no" value="" id="s_number" /><br>
姓名 <input type="text" placeholder="姓名" name="name" id="s_name" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
<script>
function sNameCheck(){
var s_num= document.getElementById("s_number");
var reg = /15007301((0[1-9])|([1-3][0-9])|(4[0-2]))/;
if(s_num.value.match(reg)!=null)
{
return true;
}
else
{
alert( s_num.value+"is fail");
return false;
}
}
</script>
?>