前端小白之每天学习记录----php(3)

1.链接数据库:

新建.php文件

<meta charset="UTF-8">
<?php
    /*
        arg1:  主机(localhost)    
        arg2: 用户名(user_name)
        arg3: 密码(user_password)
    */
    $link = mysql_connect( "localhost", "root", "root" );
    // var_dump( $link );
    if( $link ){
        echo '数据库连接成功</br>';
    }else {
        echo '数据库连接失败,警告内容:' . mysql_error();
    }
    //强制终止程序的执行, 并输出提示信息
    die( '程序在这里挂掉了' );
    echo '222';
?>

2.执行插入语句

<meta charset="UTF-8">
<?php
    //第一步: 连接数据库--star--
    $link = mysql_connect( "localhost", "root", "root" );
    if( !$link ){
        die( '数据库连接失败, 警告内容:' . mysql_error() );
    }
    // 连接数据库--end--


    //数据库如果连接成功, 程序往下执行
    //用php的方式 对数据库的方式 进行增删查改(CURD操作)
    //curd: c( created:创建,插入 )  u( update:修改,更新 )   r( read:读取,查询 )    d( delete:删除 )
    // c---->insert语句    u--->update语句  r--->select语句    d--->delete语句

    //指定链接的数据库名company_info
    mysql_select_db( "company_info" );

    //指定当前插入数据库的数据 用的编码
    mysql_query("set names utf8");

    //插入数据
    // $sql = 'INSERT INTO user_info( user_name, user_pwd) VALUES( "Beijing", "shenzhen123" )';
    //在user_info表单里面的user_name,user_pwd列分别添加内容;
    $sql = 'INSERT INTO user_info( user_name, user_pwd) VALUES( "第一个insert", "深圳123" );';
    mysql_query( $sql );  //执行插入语句
    echo mysql_insert_id() . '<br/>'; //加入的信息对应的索引,最后一行的索引1

    //插入第二条数据
    $sql = 'INSERT INTO user_info( user_name, user_pwd) VALUES( "第二个insert", "深圳123" );';
    mysql_query( $sql );
    echo mysql_insert_id() . '<br/>'; //2

    //插入多条数据
    // $sql = 'INSERT INTO user_info( user_name, user_pwd ) VALUES ( "first", "first"), ("second", "second"), ("three", "three")';
    //调试的时候用die();
    // echo $sql;die();

    /*语句写完了, 需要用php执行sql语句
    $res = mysql_query( $sql );
    var_dump( $res );
    if( $res !== false ){
        echo '插入成功:' . mysql_insert_id();
    }*/

?>

3.超级全局变量   $_GET

默认情况下, 表单里面输入的数据 是通过 超级全局数组(变量)$_GET 获取

 超级全局变量:  
            1,php自带的,不用去定义它
            2,php任何页面,任何地方 都能访问到$_GET
当提交表单的时候, $_GET就能够获取到整个表单的数据
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <form action="">
      <p>
          用户名:
          <input type="text" name="userName" value="<?php echo $_GET['userName']; ?>">
      </p>
      <p>
          <input type="submit" value="注册">
      </p>
  </form>
  <?php

    // print_r( $_GET );

    //isset: 判断一个变量 是否存在
    // var_dump( isset( $_GET['userName'] ) );


    //empty: 判断变量的值 是否为空
    // var_dump( empty( $_GET['userName'] ) );

    print_r( $_GET );

  ?>
</body>
</html>

4.简单的注册账号

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <!--action: 提交表单数据 要跳转到的页面, 如果没有写, 就是把表单数据交给本页面处理  -->
  <form action="">
    <p>
      用户名:
      <input type="text" name="userName">
    </p>
    <p>
      密码:
      <input type="password" name="pwd">
    </p>
    <p>
      <input type="submit" value="注册">
    </p>
  </form>
  <?php
/*
判断 有没有点击注册按钮
*/


$link = mysql_connect( "localhost", "root", "root" );
if( !$link ){
    die( mysql_error() );
}
mysql_select_db("company_info");
mysql_query("set names utf8");

//用户名和密码都不为空
if( !empty( $_GET['userName']) && !empty( $_GET['pwd'] ) ){
    $userName = $_GET['userName'];
    $userPwd = $_GET['pwd'];
    
    //查询数据(用户名)
    $sql = 'SELECT * FROM user_info WHERE user_name = "' . $userName . '"';
    $res = mysql_query( $sql );
    
    //mysql_num_rows: 语句后跟的参数是 资源类型, 而一般参数后面跟的参数是 查询语句的返回值
    // 它的作用是: 返回这个资源对应的结果数量(有多少个)
    // echo mysql_num_rows( $res );
    // die();
    if( mysql_num_rows( $res ) > 0 ){//用户名是否重复
        echo '<script>alert("该用户已经存在,请选择另一个");</script>';
    }else {
        //插入语句
        $sql = 'INSERT INTO user_info(user_name, user_pwd) VALUES ( "' . $userName . '",' . '"' . $userPwd . '"' . ')';
        $res = mysql_query( $sql );
        if( $res !== false ){
            echo '<script>alert("用户名注册成功");</script>';
        }else {
            echo '<script>alert("用户名注册失败");</script>';
        }
    }
}
?>
</body>
</html>

5.提交的方式 GET与POST

<!--表单的默认提交方式是get, 还有一种常见方式是:post  -->
  <form action="" method="get">
    <p>
      用户名:
      <input type="text" name="userName">
    </p>
    <p>
      密码:
      <input type="password" name="pwd">
    </p>
    <p>
      <input type="submit" value="注册">
    </p>
  </form>

  <?php
  //2048 x 1000 x 1000
  /*
    1,post比get安全  
    2,post方式提交的数据比get方式大
  */
    //如果表单用的是post方式, $_GET方式 是 获取不到post方式提交的数据
    // print_r( $_GET );
    // print_r( $_POST );

    //$_REQUEST: 可以同时用来接收 get或者post的数据
    print_r( $_REQUEST );

  ?>

6.插入数据,删除数据,修改数据

<?php
    /*
        小结:    
            insert, delete, update
            以上3种操作, 用mysql_query执行的时候, 返回都是布尔值
            
            如果是select语句
            用mysql_query查询出来的是 资源类型
    */



    $link = mysql_connect( "localhost", "root", "root" );
    if( !$link ){
        echo '数据库连接失败:' . mysql_error();
    }
    
    mysql_select_db( "company_info" );
    mysql_query("set names utf8");
   //插入 // $sql = 'INSERT INTO user_list( name, age, sex ) VALUES ( "用户名", 22, "man" )'; // if( mysql_query( $sql ) !== false ){ // echo '执行成功:' . mysql_insert_id(); // }    //删除 // $sql = 'DELETE FROM user_list WHERE user_id = 1'; // if( mysql_query( $sql ) !== false ){ // echo 'delete 成功;'; // }   //修改 $sql = 'UPDATE user_list SET name = "吴京", age = 40, sex = "男" WHERE user_id = 2'; if( mysql_query( $sql ) !== false ){ echo '更新成功;'; } ?>

7.四种查询数据方法 (SELECT语句)

mysql_fetch_assoc
mysql_fetch_row
mysql_fetch_array
mysql_fetch_object
<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row"> <?php $link = mysql_connect("localhost", "root", "root"); if( !$link ){ die(mysql_error()); } mysql_select_db("company_info"); mysql_query("set names utf8"); //别名 可以用as 也可以不用as $sql = 'SELECT user_id as uId,user_name uName,user_pwd uPwd FROM user_info'; $res = mysql_query( $sql ); // var_dump( $res ); echo "<table class='table table-bordered table-striped table-hover'>"; echo "<tr><th>用户id</th><th>用户名</th><th>用户密码</th></tr>"; //$row: 保存每次循环出来的数据 /* 数组索引(键,下标) mysql_fetch_assoc 读出来的数据(数组) (数组的索引是列名关联索引) [user_id] => 1 [user_name] => 用户名 [user_pwd] => 密码 */ //四种查数据方法 // 第一种.mysql_fetch_assoc // while( $row = mysql_fetch_assoc( $res ) ){ // // print_r( $row ); // // echo $row['user_id'] . '--->' . $row['user_name'] . '---->' . $row['user_pwd'] . '<br/>'; // echo "<tr>"; // // echo "<td>{$row['user_id']}</td><td>{$row['user_name']}</td><td>{$row['user_pwd']}</td>"; // echo "<td>{$row['uId']}</td><td>{$row['uName']}</td><td>{$row['uPwd']}</td>"; // echo "</tr>"; // } // 第二种. mysql_fetch_row // while( $row = mysql_fetch_row( $res ) ){ // // print_r( $row ); // // echo $row['user_id'] . '--->' . $row['user_name'] . '---->' . $row['user_pwd'] . '<br/>'; // echo "<tr>"; // // echo "<td>{$row['user_id']}</td><td>{$row['user_name']}</td><td>{$row['user_pwd']}</td>"; // echo "<td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td>"; // // echo "</tr>"; // } // echo "</table>"; // 第三种:mysql_fetch_array //MYSQL_NUM: 过滤出数字索引的结果 //MYSQL_ASSOC: 过滤出非数字索引的结果 // while( $row = mysql_fetch_array( $res, MYSQL_ASSOC ) ){ // print_r( $row ); // // echo $row['user_id'] . '--->' . $row['user_name'] . '---->' . $row['user_pwd'] . '<br/>'; // echo "<tr>"; // echo "<td>{$row['uId']}</td><td>{$row['uName']}</td><td>{$row['uPwd']}</td>"; // // echo "<td>{$row[0]}</td><td>{$row[1]}</td><td>{$row[2]}</td>"; // echo "</tr>"; // } // echo "</table>"; // 第四种:mysql_fetch_object while( $row = mysql_fetch_object( $res ) ){ // print_r( $row ); // echo $row['user_id'] . '--->' . $row['user_name'] . '---->' . $row['user_pwd'] . '<br/>'; echo "<tr>"; // echo "<td>{$row['uId']}</td><td>{$row['uName']}</td><td>{$row['uPwd']}</td>"; echo "<td>{$row->uId}</td><td>{$row->uName}</td><td>{$row->uPwd}</td>"; echo "</tr>"; } echo "</table>"; ?> </div> </div> </body> </html>
原文地址:https://www.cnblogs.com/szlxb/p/7305748.html