php mysql中的ajax数据响应始终为0

php mysql中的ajax数据响应始终为0,php,mysql,ajax,Php,Mysql,Ajax,我想通过ajax php将一些数据从mysql加载到div。自动递增的id字段始终等于零 我试过使用get、post等,但都不起作用 <script> $(document).ready(function(){ $(document).on('click', '#getUser', function(e){ e.preventDefault(); var uid = $(this).data("customer_id"); // it will g

我想通过ajax php将一些数据从mysql加载到div。自动递增的id字段始终等于零

我试过使用get、post等,但都不起作用

<script>
  $(document).ready(function(){

   $(document).on('click', '#getUser', function(e){

    e.preventDefault();

    var uid = $(this).data("customer_id");   // it will get id of clicked row

    $('#txtHint').html(''); // leave it blank before ajax call
    $('#mySidenav').show();      // load ajax loader

    $.ajax({
      url: 'getCustomerDetails.php',
      type: 'GET',
      data: 'customer_id='+uid,
      dataType: 'html'
    })
    .done(function(data){
      console.log(data);  
      $('#txtHint').html('');    
      $('#txtHint').html(data); // load response 
      $('#modal-loader').hide();      // hide ajax loader 
    })
    .fail(function(){
      $('#txtHint').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
      $('#modal-loader').hide();
    });

  });

 });

</script>


        <?php

include_once('../../config/dbconfig.php');


    if (isset($_REQUEST['customer_id'])) {

        $id = intval($_REQUEST['customer_id']);
        $query = "SELECT * FROM customers WHERE customer_id=:id";
        $stmt = $pdo->prepare( $query );
        $stmt->execute(array(':id'=>$id));
        $row=$stmt->setFetchMode(PDO::FETCH_ASSOC);


        ?>
        <div class="row">
            <div class="col-md-1">
            <div class="col-md-4" >
                <?php echo $row['first_name'];?>
            </div>
            <div class="col-md-6">

            </div>
        </div>
    </div>


    <?php echo $id;?><br/>


<?php
}

?>
结果不是回显第一个名称,因为$id始终为0。 我想获得个人$idauto_增量
完成后,应显示用户记录

从调试数据库中的实际结果开始

if (isset($_REQUEST['customer_id'])) {

        $id = intval($_REQUEST['customer_id']);
        $query = "SELECT * FROM customers WHERE customer_id=:id";
        $stmt = $pdo->prepare( $query );
        $stmt->execute(array(':id'=>$id));
        $row=$stmt->setFetchMode(PDO::FETCH_ASSOC);
您没有检查错误

两项建议:

1您正在使用。如果你检查了结果集,你会发现它有什么问题。只需使用print\r等将结果输出到错误命名的$row变量中。我相信你会看到哪里出了问题

2我强烈建议不要使用$\u请求。它懒惰而且容易出错。你知道“客户id”来自哪里吗?一场曲奇邮递还是得到?如果您通过GET=>使用GET传递信息,我假设customer\u id是数据库中的唯一键,并且数据库中的每个id只返回一行。那么,如何正确使用PDO语句:

$sql = "SELECT * FROM customers WHERE customer_id=:id";
//Prepare your SELECT statement.
$statement = $pdo->prepare($sql);
//The Primary Key of the row that we want to select.
$id = intval($_REQUEST['customer_id']);
//I highly recomment not to use $_REQUEST, use $_GET or even better $_POST

//Bind our value to the paramater :id.
$statement->bindValue(':id', $id);

//Execute our SELECT statement.
$statement->execute();

//Fetch the row.
$row = $statement->fetch(PDO::FETCH_ASSOC);

//If $row is FALSE, then no row was returned.
if($row === false){
    echo $id . ' not found!';
} else{
    echo 'Found: ' . $row['first_name'];
}
编辑 另外,按如下方式更改ajax请求:

$.ajax({
  url: 'getCustomerDetails.php',
  type: 'POST',
  data: {customer_id:uid}
})

仅供参考:您只设置了fetchMode并执行了查询,实际上没有进行任何获取。请参见$row=$stmt->fetch\u assoc;而不是$row=$stmt->setFetchModePDO::FETCH_ASSOC;fetch_assoc正在错误调用未定义的方法PDOStatement::fetch_assoc-@DejvidAgree同意一切,只是澄清一下。为什么$row变量名是错误的?是因为它太模糊了吗?我不是OP@Dejvid我这么说是因为他没有获取行,只是设置了获取模式。你的评论清楚地告诉了他。向上投票。