php - 我如何将 php 变量放入 mysql 命令中

标签 php mysql sql ajax

$id1 = $_POST['number']; //here i am getting a php variable called number

$result = mysqli_query($con, "SELECT id,main FROM first WHERE ?how can i put that number right here?");

我使用ajax从另一个页面获取变量,我应该将其放入mysql命令中。我该怎么做?

最佳答案

以下是使用准备好的语句的示例:

   /* Create a new mysqli object with database connection parameters */
   $mysqli = new mysql('localhost', 'username', 'password', 'db');

   if(mysqli_connect_errno()) {
      echo "Connection Failed: " . mysqli_connect_errno();
      exit();
   }

   /* Create a prepared statement */
   if($stmt = $mysqli -> prepare("SELECT priv FROM testUsers WHERE username=?
   AND password=?")) {

      /* Bind parameters
         s - string, b - blob, i - int, etc */
      $stmt -> bind_param("ss", $user, $pass);

      /* Execute it */
      $stmt -> execute();

      /* Bind results */
      $stmt -> bind_result($result);

      /* Fetch the value */
      $stmt -> fetch();

      echo $user . "'s level of priviledges is " . $result;

      /* Close statement */
      $stmt -> close();
   }

   /* Close connection */
   $mysqli -> close();

Source

我强烈建议尽早研究准备好的陈述。

关于php - 我如何将 php 变量放入 mysql 命令中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20472915/

相关文章:

PHP stream_socket_client 连接失败

PHP文字转表情

php - 将 PHP/MySQL 与 Google map 结合使用

mysql - 如何在mysql中使用一个SQL查询选择多个静态行

c# - SQL LIKE 命令不包括某些数据

php - 对超过 100 万行的表运行查询

php - 在 WordPress 插件(PHP CRUD)中进行多个数据库调用

php - 我应该如何使用外键导入mysql数据库中的单独表?

PHP password_verify 不适用于数据库

php - 使用 foreach from 变量将多行插入数据库