zoukankan      html  css  js  c++  java
  • 新版本PHP使用更方便了

    $hostname_conn = "";
    $database_conn = "";
    $username_conn = "root";
    $password_conn = "";
    $conn =mysqli_connect($hostname_conn, $username_conn, $password_conn, $database_conn);

    <?php
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    // Check connection
    if (mysqli_connect_errno($con))
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    // Perform queries 
    mysqli_query($con,"SELECT * FROM Persons");
    mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) 
    VALUES ('Glenn','Quagmire',33)");

    mysqli_close($con);
    ?>

    <?php
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    // Check connection
    if (mysqli_connect_errno($con))
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
    $result=mysqli_query($con,$sql)

    // Associative array
    $row=mysqli_fetch_assoc($result);
    printf ("%s (%s)n",$row["Lastname"],$row["Age"]);

    // Free result set
    mysqli_free_result($result);

    mysqli_close($con);
    ?>

    php mysqli query 查询数据库后读取内容的方法

    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

    /* check connection */
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s ", $mysqli->connect_error);
        exit();
    }

    /* Create table doesn't return a resultset */
    if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
        printf("Table myCity successfully created. ");
    }

    /* Select queries return a resultset */
    if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
        printf("Select returned %d rows. ", $result->num_rows);

        /* free result set */
        $result->close();
    }

    /* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
    if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

        /* Note, that we can't execute any functions which interact with the
           server until result set was closed. All calls will return an
           'out of sync' error */
        if (!$mysqli->query("SET @a:='this will not work'")) {
            printf("Error: %s ", $mysqli->error);
        }
        $result->close();
    }

    $mysqli->close();
    ?>

    实例代码:

    $sql="select * from qa limit 0,10";
     $result=$mysqli->query($sql);
     while ($row = mysqli_fetch_assoc($result))
      {
        echo "question_title : {$row['question_title']} <br>";
        echo "answer_content1: {$row['answer_content1']} <br>";
      }
     /* free result set */
        $result->close();

  • 相关阅读:
    又见JavaWeb的中文乱码
    Java 中文字符串编码之GBK转UTF-8
    关于Java项目打包
    FreeSWITCH无法读取wav文件
    CentOS 7 最小化安装的网络配置
    虚拟机无法使用网卡桥接模式
    阿里云建站流程
    Spring MVC无法获取ajax POST的参数和值
    Mysql ERROR 1064 (42000)
    eclipse创建java类时自动添加注释
  • 原文地址:https://www.cnblogs.com/chenjian/p/4784068.html
Copyright © 2011-2022 走看看