gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:56:56 25 4
gpt4 key购买 nike

$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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com