首页博客网络编程
使用 MySQL:选择数据库条目
摘要 使用 MySQL:选择数据库条目

PHP MySQL SELECT 用于访问 MySQL 数据库中的记录。

内容

在 MySQL 中选择数据库条目

复制

<?php
  echo "<table>";  echo "<tr><th>Id</th><th>Name</th><th>Surname</th></tr>";  class table_rows extends recursive_array_iterator {    function __construct($it) {      parent::__construct($it, self::LEAVES_ONLY);
    }  	    
    function current_row() {      return "<td>" . parent::current(). "</td>";
    }    function begin_children() {      echo "<tr>";
    }    function end_children() {      echo "</tr>" . "\n";
    }
  } 
  $server = 'server';
  $user = 'user';
  $pass = 'pass';
  $db = 'db';  	
  try {
    $con = new PDO("mysql:host=$server;dbname=$db", $user, $pass);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    //prepare the statement
    $statement = $con->prepare("SELECT user_id, name, surname FROM users");    // make the query array associative
    $statement->execute();
    $result = $statement->setFetchMode(PDO::FETCH_ASSOC);    //iterate through the queried data
    foreach(newtable_rows(newrecursive_array_iterator($stmt->fetchAll())) as $k => $v) {      echo $v;
      }
  } catch(PDOException $e) {    //set up an error message
    echo "Error message: " . $e->getMessage();
  }
  $con = null;  echo "</table>";?>

上面的查询示例从数据库中选择特定条目。或者,您可以使用星号 (*) 选择表格中的每一列。

注意:将所有 SQL 语句存储在一行代码中可确保它们正常运行。

使用 MySQLi 使 MySQL 选择数据库条目

在下面的示例中,我们在 users 表中选择 user_idname 和 surname 列,并将其显示在我们的网页上:

复制

<?php
  $server = 'server'; 
  $user = 'user'; 
  $pass = 'pass'; 
  $db = 'db';  // establish connection
  $con = new mysqli($server, $user, $pass, $db);  // see if connection has been established properly
  if ($conn->connect_error) { 
    die("Failed to connect: " . $con->connect_error);
  } 
  $sql = "SELECT user_id, name, surname FROM users";
  $result = $con->query($sql);  if ($result->num_rows > 0) {    // display data from the query
    while($row = $result->fetch_assoc()) {      echo "User Id: " . $row["user_id"]. " - Name: " . $row["name"]. " - Surname: " . $row["surname"]. "<br/>";
    }
  } else {    echo "0 results";
  }
  $con->close();  
?>

首先,我们必须编写 PHP SQL 查询,选择 users 表中的user_id名称和姓氏列。下一个代码行执行了一个 PHP SQL 查询,并将请求的数据放入我们称之为 .$result

完成此操作后,运行我们的结果数组以查看是否返回了任何变量。如果结果集中存在任何行,则该函数会将所有这些行放在可以轻松循环的关联数组中。循环用于遍历结果数组。它显示user_id姓名姓氏列的结果。num_rows()fetch_assoc()while()

下面的示例显示了如何使用 MySQLi 中的过程方法执行 PHP MySQL 查询:

复制

<?php
  server = 'server'; 
  $user = 'user'; 
  $pass = 'pass'; 
  $db = 'db';  // establish connection
  $con = mysqli_connect($server, $user, $pass, $db);  // see if the connection has been established properly
  if (!$con) {    die("Failed to connect: " . mysqli_connect_error());
  }
  $sql = "SELECT user_id, name, surname FROM users";
  $result = mysqli_query($con, $sql);  if (mysqli_num_rows($result) > 0) {    while ($row = mysqli_fetch_assoc($result)) {      // display data from the query
      echo "User Id: " . $row["user_id"]. " - Name: " . $row["name"]. " - Surname: " . $row["surname"]. "<br>"; 
      }
  } else {     echo "0 results";
  }
  mysqli_close($con);  
?>

下一个示例演示如何将数据库条目显示为表。

复制

<?php
  $server = 'server';
  $user = 'user';
  $pass = 'pass';
  $db = 'db';  // establish connection
  $con = new mysqli($server, $user, $pass, $db);  // see if the connection has been established properly
  if ($con->connect_error) {    die("Failed to connect: " . $conn->connect_error);
  }
  $sql = "SELECT user_id, name, surname FROM users";
  $result = $con->query($sql);  if ($result->num_rows > 0) {    echo "<table><tr><th>User Id</th><th>Name</th></tr>";    while ($row = $result->fetch_assoc()) {      // display data from the query
      echo "<tr><td>".$row["user_id"]."</td><td>".$row["name"]." ".$row["surname"]."</td></tr>";
    }    echo "</table>";
  } else {    echo "0 results";
  }
  $con->close();  
?>

用于 PHP MySQL 选择的 PDO 和预准备语句

我们将使用预准备语句使MySQL选择数据库条目。

选择用户表中的user_id姓名姓氏列,并将其显示在表中:

复制

<?php
  echo "<table>";  echo "<tr><th>Id</th><th>Name</th><th>Surname</th></tr>";  class table_rows extends recursive_array_iterator {    function __construct($it) {      parent::__construct($it, self::LEAVES_ONLY);
    }  	    
    function current_row() {      return "<td>" . parent::current(). "</td>";
    }    function begin_children() {      echo "<tr>";
    }    function end_children() {      echo "</tr>" . "\n";
    }
  } 
  $server = 'server';
  $user = 'user';
  $pass = 'pass';
  $db = 'db';  	
  try {
    $con = new PDO("mysql:host=$server;dbname=$db", $user, $pass);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    //prepare the statement
    $statement = $con->prepare("SELECT user_id, name, surname FROM users");    // make the query array associative
    $statement->execute();
    $result = $statement->setFetchMode(PDO::FETCH_ASSOC);    //iterate through the queried data
    foreach(newtable_rows(newrecursive_array_iterator($stmt->fetchAll())) as $k => $v) {      echo $v;
      }
  } catch(PDOException $e) {    //set up an error message
    echo "Error message: " . $e->getMessage();
  }
  $con = null;  echo "</table>";?>


声明提示:若要转载请务必保留原文链接,申明来源,谢谢合作!

本文链接:https://www.gaoxuejun173.top/blog/182

广告位

本文配乐
来说两句吧

该文章已禁止评论

最新评论

广告位