JS day14 - PHP and MySQL

1.PHP:

php installation:

    Install - double click exe  —— Find the installed folder - double click the executable exe - click start

1. Double click exe:

 

2. After installation, execute exe and click Start:

3. When the status is green, the installation is successful:

 

  php application:

How to access files through the web server (apache):

1. Place the contents to be accessed in the phpstudy folder on the disk   www folder

2. The access method is   Native ip address + file path

Note: local IP = = all paths before the WWW file

2.php syntax:

*******In addition to being a back-end logical file, php can also be used as an html page.

1.php write the code in the. php file.

2. The code should be written in <? php     And   ?>  between.

3. Each line of code must end with a semicolon.

4. Three annotation methods of PHP://     or   #  or     /* Content*/   .

5.echo is equivalent to document.write.

6. Make php support Chinese encoding format:

      header("Content-type:text/html;charset=utf-8");

7. Ring address:

      http://127.0.0.1/testPhp/HelloWorld.php

      http://localhost/testPhp/HelloWorld.php

<?php
	//One of the functions of echo is document.write
	
	//Support Chinese coding format
	header("Content-type:text/html;charset=utf-8");
	
	echo "Lao Wang";
	echo "<br>";
	echo "dark green";
?>

1. Variable definition: add string sequence starting with $

$name = "Lao Wang";
$name1 = "Dull hair";
echo "$name";

2.php string splicing uses

echo $name ." ". $name1;

3.if conditional statement:

Any variable name must be written with the $character

$a=123;
$b=456;
$c;
if($a>$b){
    $c = $a;
}else{
    $c = $b;
}
echo $c;//456

4. Loop and array:

count (array): returns the length of the array

// $arr = [1,2,4,5,6];
$arr=Array(1,2,3,4,6);
for($i=0;$i<count($arr);$i++){
    echo $arr[$i] . "<br>";
}

5. Function:

function add($a,$b){
    return $a+$b;
}

echo add(1,2);//3

3. Form submission:

    action: submit data file

     Method: data submission method

        1.get: low security, high efficiency and small carrying data. get will carry the request parameters after the url address

        2. post: high security, low efficiency and large carrying data  

     name: agreement of front and back end interaction

    1. html file content:

 <form action="zhishi.php" method="GET">
        user name:<input type="text" name="userName"><br>
        password:<input type="text" name="userPwd"><br>
        <input type="submit" value="Sign in">   
    </form>

******php receives front-end data  

      1. Two by two correspondence. The front end uses what it uses, and the back end uses what it receives

           $_ GET[key value];

           $_ POST[key value];

      2. Both are acceptable

          $_ request[key value];  

2. php file content:

<?php

header("Content-type:text/html;charset=utf-8");

$name = $_GET["userName"];
$pwd = $_GET["userPwd"];
echo $name ."".$pwd;

?>

4. Database

1. Database concept:

*    Warehouse: Warehouse

*    Table: a warehouse is divided into many parts, much like classes

*    Fields: much like each attribute of a class.

*    Data type of each field:

      Database is a warehouse that organizes, stores and manages data according to data structure.

       For example:

   Int ----- > integer blob ----- > binary data

  Varchar / char ----- > string

   Date ----- > date

2. Common databases

    * Relational database: Oracle, MySQL, SQLServer, DB2, sybase

    * Non relational databases: Redis, Tokyo Cabinet, Cassandra, Voldemort, MongoDB, Dynomite,

                      HBase,CouchDB,Hypertable, Riak,Ti,

3. Database operation:

      1.MYSQL Manager - select MySQL front

      2. User login - log in as root by default, and the logged in user name is localhost

      There are many libraries under a user name, and there are many tables under each library

              Object Browser: describes the type and number of fields

              Data browser: describes records

              SQL compiler: operate the database through SQL statements

      3. Create database

      4. Open a library

      5. Create table (in SQL compiler)

  4. Creation, addition, deletion, modification and query of SQL statement database - in the database:

    1. Create table:

create table name (field 1, field 2..... Field n);

create table student
(
      stu_id int,
      stu_nume varChar(10),
      stu_age int,
      stu_gender char(2)
);

      2. Add a record

   insert into table name (field 1, field 2..... Field n)
    Values (value 1, value 2..... Value n);

insert into student (stu_id,stu_nume,stu_age,stu_gender)
values(1, "Dull hair" , 22 , "M" );

insert into student 
values(2,"Hee hee",21,"w");

insert into student 
values(2,"ha-ha",21,"w");

insert into student 
values(2,"Hoo hoo",21,"w");

      3. Delete:

          delete from table name;

        * Delete all contents in student:

delete from student;

        where clause:

           * Delete stu_ All contents with ID 1:

delete from student
where stu_id=1;

        AND  OR:

          * Delete stu_id 1 and stu_nume is all the contents of dead hair:

delete from student
where stu_id=1
AND stu_nume="Dull hair";

        Delete entire table:

qrop table student;

        4. Change

update table name: set field 1 = value 1... Field n = value n;

     * Modify the entire:

update  student set stu_nume="a mandarin orange";//Replaced all the names with oranges

     * Modified part: add where

update  student set stu_nume="a mandarin orange",stu_age=22
where stu_nume="Dull hair";//Change the name from dumao to orange and the age to 22

        5. Check

select field 1... Field n from table name;

      * Check part:

select stu_nume from student;//Find all names in the table

       * Check all:

select * from student;//Find out all the contents in the table

5.php connection to mySQL: (control the database in the php file)

  1. Log in to the database and create a connection object (there is a user after logging in)

mysql_connect("address of database server", "user name", "password"): the return value is the connection object

$conn = mysql_connect("localhost","root","root");

2. Select database:

mysql_select_db("database name");

3. Data operation: add, delete, modify and query

  mysql_query(sql statement, connection object);

  1. Increase

mysql_query("insert into student values (3,'Lily',19,'M')",$conn);

  2. Delete

mysql_query("delete from student where stu_nume='Dull hair'",$conn);

3. Change

mysql_query("update student set stu_nume='Huanhuan' where  stu_id=3",$conn);

 ****** 4. Query (return result set, similar to a table)

mysql_ num_ Rows: returns the number of records corresponding to the current result set

    1. It is usually used as the judgment condition for login and registration

 //Judge whether there is stu in the corresponding database_ Nume is the information of dead hair. If there is any, the login is successful, and if there is no, the login fails
$result = mysql_query("select * from student  where stu_nume='Dull hair'",$conn);
           if(mysql_num_rows($result)==1){
               echo "Login succeeded";
           }else{
               echo "Login failed";
           }

    2. It is used to obtain a record in the database

  musql_ fetch_ Assoc (result set): returns the record pointed to by the current cursor and stores it as an object

            Note: MySQL_ fetch_ After Assoc method is executed once, the cursor will automatically move down

//Displays all the contents of the database on the page
 $result = mysql_query("select * from student ",$conn);
           while($obj = mysql_fetch_assoc($result)){
               echo $obj["stu_id"]."".$obj["stu_nume"]."".$obj["stu_age"]."".$obj["stu_gender"].""."<br>";
           }

4. Close the connection object:

mysql_ Close (connection object);

mysql_close($conn);//close

6. Simulated Login or registration:

html page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="denglu.php" method="POST">
        ID: <input type="text" name="useid"><br>
        NAME: <input type="text" name="useName"><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

php page:

<?php
//Support Chinese coding format
  header("Content-type:text/html;charset=utf-8");

  //Get the id and name of the html file submission
  $id = $_POST["useid"];
  $name=$_POST["useName"];
  //Login database to create connection object
  $conn = mysql_connect("localhost","root","root");
  //If the connection is successful
  if($conn){
     //Select the appropriate database
      mysql_select_db("2021-9-26");
     //Find out whether the obtained id and name exist in the database
      $result =mysql_query("select * from student where stu_id=$id AND stu_nume='$name'",$conn);
      //If it exists, the user name already exists
      if(mysql_num_rows($result)==1){
          echo "User name already exists";
      }else{
      //If it does not exist, register the corresponding data
          echo "login was successful";
          mysql_query("inser into student values ($id,'$name',20,'M')",$conn);
      }
      //Close connection object
      mysql_close($conn);
  }
?>

Keywords: PHP Javascript MySQL

Added by Nameless12 on Mon, 27 Sep 2021 15:03:01 +0300