• PhP访问mysql数据库的基本方式


    一,查询

    <?php
    $conn= mysql_connect("127.0.0.1","lanou12","lanou12");
    $fieldName=$_GET["name"];
    if($conn)
    {
    mysql_select_db("Class_12_Lesson");
    $sql="select ".$fieldName." from team_five;";
    $result= mysql_query($sql);

    $json=array();
    $j=0;
    while($row=mysql_fetch_array($result))
    {
    //dic
    $item=array();
    for($i=0;$i< mysql_num_fields($result);$i++)
    {
    $key=mysql_field_name($result, $i);
    $value=$row[$key];
    $item[$key]=$value;
    }
    //item put in json
    $json[$j]=$item;
    $j++;
    }
    echo(json_encode($json));
    }
    else
    {
    echo("connect error!");
    }

    mysql_close($conn);
    ?>

    二,插入

    <?php
    $conn= mysql_connect("127.0.0.1","lanou12","lanou12");
    if($conn)
    {

    mysql_select_db("Class_12_Lesson");
    $sql="insert into team_five(student_name,student_age) values('lichanghong',25)";

    $result= mysql_query($sql);
    if($result)
    {
    echo("successfully...");
    }
    else
    {
    echo("insert failed".$result);
    }
    }
    else
    {
    echo("connect error!");
    }
    mysql_close($conn);
    ?>

  • 相关阅读:
    Codeforces 672D
    Codeforces 672C
    Codeforces 673D
    Codeforces 1181D
    Codeforces 1181C
    Codeforces 1011F
    Codeforces 1011E
    2020.1.3计导全面复习
    csp模拟题-201903
    CCF模拟题-201909
  • 原文地址:https://www.cnblogs.com/huntaiji/p/3478742.html
Copyright © 2020-2023  润新知