gpt4 book ai didi

PHP - 将变量绑定(bind)到准备好的语句时出错

转载 作者:行者123 更新时间:2023-11-29 05:50:34 25 4
gpt4 key购买 nike

尝试将变量 $user_id 绑定(bind)到准备好的语句,数组作为 jsonarray 发送到 java 文件。下面的代码工作得很好,但它没有绑定(bind)参数,其中 qual_id 的值是静态的。

if ($user_id->num_rows >= 1) {
$mysql_qry = "select * from user_qualification where qual_id='1'";
//$stmt = mysqli_stmt_init($conn);
//$result = mysqli_stmt_prepare($stmt, $mysql_qry);
//mysqli_stmt_bind_param($result, "i", $user_id);

$result = mysqli_query($conn,$mysql_qry);
//mysqli_stmt_execute($stmt);

$data_item = array();
while($row = mysqli_fetch_assoc($result)){
array_push($data_item,
array('u_school'=>$row['school'],
'u_hschool'=>$row['hschool'],
'u_undergrad'=>$row['ugrad'],
'u_grad'=>$row['grad'],
'u_phd'=>$row['phd'],
)
);
}

以下代码包含与绑定(bind)参数有关的错误:

if ($user_id->num_rows >= 1) {
$mysql_qry = "select * from user_qualification where qual_id='?'";
$stmt = mysqli_stmt_init($conn);
$result = mysqli_stmt_prepare($stmt, $mysql_qry);
mysqli_stmt_bind_param($result, "s", $user_id);

//$result = mysqli_query($conn,$mysql_qry);
mysqli_stmt_execute($stmt);

$data_item = array();
while($row = mysqli_fetch_assoc($result)){
array_push($data_item,
array('u_school'=>$row['school'],
'u_hschool'=>$row['hschool'],
'u_undergrad'=>$row['ugrad'],
'u_grad'=>$row['grad'],
'u_phd'=>$row['phd'],
)
);
}

qual_id 列也是 bigint。

编辑 1:
去掉占位符的引号。
$mysql_qry = "select * from user_qualification where qual_id=?";

编辑 2 :
从“s”更改为“i”。
mysqli_stmt_bind_param($result, "i", $user_id);

编辑 3 :
var_dump 变量 $user_id 在 if 语句之后
object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1 ) ["类型"]=> 整数(0) } []

编辑 4 :
php文件

<?php
require "conn.php";
$user_name ="omx123"; //$_POST["user_name"];
if ($mysql_qry = $conn->prepare("Select id from UserLoginDetails where username=?")) {
$mysql_qry->bind_param("s", $user_name);
$mysql_qry->execute();
$user_id = $mysql_qry->get_result();

if ($user_id->num_rows >= 1) {
var_dump($user_id);
$mysql_qry = "select * from user_qualification where qual_id=?";
$stmt = mysqli_stmt_init($conn);
$result = mysqli_stmt_prepare($stmt, $mysql_qry);
mysqli_stmt_bind_param($result, "i", $user_id);

//$result = mysqli_query($conn,$mysql_qry);
mysqli_stmt_execute($stmt);

$data_item = array();
while($row = mysqli_fetch_assoc($result)){
array_push($data_item,
array('u_school'=>$row['school'],
'u_hschool'=>$row['hschool'],
'u_undergrad'=>$row['ugrad'],
'u_grad'=>$row['grad'],
'u_phd'=>$row['phd'],
)
);
}
echo json_encode($data_item);
}
}
$conn->close();
?>

编辑 5:
感谢您的帮助用户:dWinder
$user_id 上的 var_dump :
整数(1)[]
但是数组仍然是空的。

编辑 6:
现在工作得很好 :D

$mysql_qry = "select * from user_qualification where qual_id=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $mysql_qry);
mysqli_stmt_bind_param($stmt, "i", $user_id_int);
mysqli_stmt_execute($stmt);
$result=mysqli_stmt_get_result($stmt);

最佳答案

在您共享 $user_id 的转储后,它似乎是 mysql 结果。为了从那里提取您的 ID,您应该使用 mysqli_fetch_assoc

if ($user_id->num_rows >= 1) {
$row = mysqli_fetch_assoc($user_id)
$user_id_int = $row["id"]; // or what ever you used to call it

// now you can call the bind...
$mysql_qry = "select * from user_qualification where qual_id=?";
$stmt = mysqli_stmt_init($conn);
$result = mysqli_stmt_prepare($stmt, $mysql_qry);
mysqli_stmt_bind_param($result, "i", $user_id_int);

我假设您在获取 $user_id

时将查询用作:“SELECT id From ...”

关于PHP - 将变量绑定(bind)到准备好的语句时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54768296/

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