📜  如何使用PHP执行 SQL 查询并获取结果?

📅  最后修改于: 2022-05-13 01:55:04.020000             🧑  作者: Mango

如何使用PHP执行 SQL 查询并获取结果?

在本文中,我们将讨论如何执行 SQL 查询以及如何获取其结果?

我们可以使用PHP mysqli_query()方法对数据库执行查询。

语法:我们可以通过两种方式使用mysqli_query()方法:

  • 面向对象的风格
  • 程序风格

参数:

  • 连接:必须指定要使用的连接。
  • 查询:也需要指定数据库查询。
  • 结果模式:可选用。

返回值:对于 SELECT、SHOW、DESCRIBE 或 EXPLAIN,它返回一个 mysqli_result 对象。对于其他成功的查询,它返回true。否则,它在失败时返回false

让我们了解如何执行 SQL 查询。

执行 SQL 查询:我们将通过示例了解如何执行 SQL 查询。我们将创建一个数据库、表,然后将一些值插入其中。

示例:创建一个数据库并向其中插入一些值。

PHP
connect_error) {
          die("Connection failed: " . $conn->connect_error);
    }
  
    $sql = "CREATE DATABASE gfgDB";
    if ($conn->query($sql) === TRUE) {
          echo "Database has been created successfully";
    } else {
          echo "Error creating database: " . $conn->error;
    }
    $conn->close();
?>


PHP
connect_error) {
              die("Connection failed: " . $conn->connect_error);
        }
  
    $sql = "CREATE TABLE Emp (
        id INT(6) AUTO_INCREMENT PRIMARY KEY,
        firstname VARCHAR(30) NOT NULL,
        lastname VARCHAR(30) NOT NULL
    )";
  
    if ($conn->query($sql) === TRUE) {
          echo "Table has been created successfully";
    } else {
          echo "Error creating table: " . $conn->error;
    }
  
    $conn->close();
?>


PHP
connect_error){
          die("Connection failed: " . $conn->connect_error);
    }
    $sql = "INSERT INTO Emp (firstname, lastname)
            VALUES ('XYZ', 'ABC')";         
  
    if ($conn->query($sql) === TRUE) {
          echo "New record created successfully";
    }else{
          echo "Error: " . $sql . "
" . $conn->error;     }     $conn->close(); ?>


PHP
connect_error) {
          die("Connection failed: " . $conn->connect_error);
    }
  
    $sql = "SELECT id, firstname, lastname FROM Emp";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"]. " - Name: " 
                . $row["firstname"]. " " . $row["lastname"]. "
";           }     }      else {           echo "No records has been found";     }     $conn->close(); ?>


输出:

Database has been created successfully

创建表:

PHP

connect_error) {
              die("Connection failed: " . $conn->connect_error);
        }
  
    $sql = "CREATE TABLE Emp (
        id INT(6) AUTO_INCREMENT PRIMARY KEY,
        firstname VARCHAR(30) NOT NULL,
        lastname VARCHAR(30) NOT NULL
    )";
  
    if ($conn->query($sql) === TRUE) {
          echo "Table has been created successfully";
    } else {
          echo "Error creating table: " . $conn->error;
    }
  
    $conn->close();
?>

输出:

Table has been created successfully

在“Emp”表中插入一些值:

PHP

connect_error){
          die("Connection failed: " . $conn->connect_error);
    }
    $sql = "INSERT INTO Emp (firstname, lastname)
            VALUES ('XYZ', 'ABC')";         
  
    if ($conn->query($sql) === TRUE) {
          echo "New record created successfully";
    }else{
          echo "Error: " . $sql . "
" . $conn->error;     }     $conn->close(); ?>

输出:

New record created successfully

注意:由于我们使用了AUTO_INCREMENT,它会自动插入“id=1”的记录,并且对于每条新插入的记录,它都会将“id”加一。

从数据库中获取结果

PHP

connect_error) {
          die("Connection failed: " . $conn->connect_error);
    }
  
    $sql = "SELECT id, firstname, lastname FROM Emp";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"]. " - Name: " 
                . $row["firstname"]. " " . $row["lastname"]. "
";           }     }      else {           echo "No records has been found";     }     $conn->close(); ?>

输出:

id: 1 - Name: XYZ ABC