MySQL没有正确过滤SELECT语句

时间:2018-04-22 20:31:49

标签: php mysql ajax

$ result-> num_rows返回值为64(所有记录)时应返回值29,而$ counter返回值0表示没有记录。我认为这是我准备好的陈述的一个问题,但我并不完全确定它可能是什么。

的Ajax

$.ajax
({
  url: 'query.php',
  type: 'POST',
  data: {Category: cat},
  //dataType: 'json',  
  success: function(result) 
  {  
    if(result) 
    {   
      console.log(result);          
    }
  },
  error: function() 
  {
    alert("error");
  }      
}); // end ajax call

PHP

<?php
require '_conn/connection.php'; 

$stmt = $conn->prepare("SELECT * FROM portfoliotb WHERE Category = ?");
$stmt->bind_param('s', $cat); 
$cat = $_POST['Category'];
$stmt->execute();

$result = $stmt->get_result();


if ($result->num_rows > 0) 
{
    echo $result->num_rows.",";
} 
else 
{
    echo "0 results";
}

$records = [];
$counter=0;

while( $row = mysql_fetch_row( $result ) )
{
    $counter++;

    //$records[] = array('Id' => $row['Id'], 'Category' => $row['Category'],'Directory' => $row['Directory'],'Description' => $row['Description'],'Code' => $row['Code']);
    //array_push( $records, $row['Category'] );   
    //array_push( $records, array("Id"=>$row['id'],"Category"=>$row['Category'],"Directory"=>$row['Directory'],"Description"=>$row['Description'],"Code"=>$row['Code'])); 
}

echo $counter;
//echo json_encode( $records );

&GT;

1 个答案:

答案 0 :(得分:0)

通过删除行

解决了这个问题
while( $row = $result->fetch_assoc() )

并将其替换为

{{1}}

感谢您的帮助