Php 为什么$stmt->;在ajax中执行()print\u r=Object
简介:我正在使用ajax制作服务器端数据表库 错误:看起来我的打印($stmt)正在返回对象。我认为这第一个错误导致了我的其他错误,比如“未捕获错误:调用成员函数fetch_assoc()on null” 此外,我认为print_r($result)没有显示任何细节 Ajax代码:Php 为什么$stmt->;在ajax中执行()print\u r=Object,php,ajax,Php,Ajax,简介:我正在使用ajax制作服务器端数据表库 错误:看起来我的打印($stmt)正在返回对象。我认为这第一个错误导致了我的其他错误,比如“未捕获错误:调用成员函数fetch_assoc()on null” 此外,我认为print_r($result)没有显示任何细节 Ajax代码: <script type="text/javascript"> $(document).on('click','.edit_btn',function (){ var id = $
<script type="text/javascript">
$(document).on('click','.edit_btn',function (){
var id = $(this).attr("id").match(/\d+/)[0];
var edit_id = $('#example').DataTable().row( id ).data();
var edit_id = edit_id[0];
$.ajax({
type:'POST',
url: 'form.php',
datatype: "json",
data: { edit_id : edit_id },
success: function(result) {
//alert(edit_id);
alert(result);
$("#edit_id").val(edit_id);
} //success func
}); //ajax
}); //end ready
</script>
$conn=mysqli_connect($host,$user,$pass,$db);
$stmt = $conn->prepare("SELECT * FROM `employees` WHERE `id` = ?");
$stmt->bind_param("i", $_POST['edit_id']);
$stmt->execute();
print_r($stmt);
$result = mysqli_query($conn, $stmt);
print_r($result);
$row = $result->fetch_assoc();
echo $row;
在php文件上,您正在使用print_r打印,这会在与DT一起使用时出现错误。首先对打印行进行注释,然后我看不到while循环将查询返回的数据插入数组,然后必须对最终的回音进行编码 由于我不知道您的员工表中的信息,让我们举一个例子: 您的查询应该如下所示:
$stmt = $conn->prepare("SELECT * FROM `employees` WHERE `id` = ?");
$stmt->bind_param("i", $_POST['edit_id']);
$stmt->execute();
// here is the change part
$result = $stm->fetchAll(); // save all returned values into $result as an object
$output = ''; // declare a variable to handle the data
// loop through all returned rows from query
foreach($result as $rows) {
$output .= '<td>'.$rows["user_name"].'</td>'
$output .= '<td>'.$rows["user_surname"].'</td>'
$output .= '<td>'.$rows["user_email"].'</td>'
$output .= '<td>'.$rows["user_phone_nr"].'</td>'
.....
.....
..... // and so on with the values you want to show (i just insert into a <td> which is supposed to be part of a table declared before the loop)
}
echo $output;
$stmt=$conn->prepare(“从'employees'中选择*,其中'id`=?”;
$stmt->bind_param(“i”、$_POST['edit_id']);
$stmt->execute();
//这是零钱
$result=$stm->fetchAll();//将所有返回值作为对象保存到$result中
$output='';//声明一个变量来处理数据
//循环查询中返回的所有行
foreach($result作为$rows){
$output.=''.$rows[“用户名”].'
$output.=''.$rows[“用户姓氏”]。'
$output.=''.$rows[“用户电子邮件”]。'
$output.=''.$rows[“用户电话号码”]。'
.....
.....
..…//以此类推,使用希望显示的值(我只是将其插入到一个循环中,该循环应该是在循环之前声明的表的一部分)
}
echo$输出;
最简单地说,您不能在Ajax中使用print\r,因为它与末尾的echo语句冲突。相反,在检查变量时必须使用var_dump 那你想要什么?mysqli_查询返回一个对象-请参阅文档:也许我需要执行$stmt=$stmt->execute();现在这等于1,但这也没有意义…请理解执行语句和运行mysqli\u查询
之间的区别。我希望查询返回数据库中的行。不是“对象”。在没有datatables库的情况下使用它时,类似的代码也可以工作。。。但我会再检查一遍……好的。这对我来说比评论更有意义。我只是暂时打印一下,以便从第二个参数$stmt中找出为什么$result很可能为null。让我给它一个问题,我看到的是$result是空的,整个ajax设置(比如最后的echo)都不重要,因为我必须先解决这个问题。好吧,那么你可能是对的,打印会弄乱ajax。现在我在bolean上获取fetch_assoc()。我会继续玩下去,不知道是谁否决了你。。。这是正确的方向。。。在使用ajax时,标记为answer.看起来var_dump($result)是正确的方法。不打印(结果);返回的是假的,这是有意义的。。。