I have an ajax code to make an table with the results that i should receive from my php table, but my php its not working. I must receive an a json encode from php with all arrays that i have in mysql.
Here's my ajax code
$(document).ready(function(){
$.ajax({
url: 'financeiro/cpg/tabela.php',
success: function(data)
{
console.log(data);
createTableByJqueryEach(data);
},
async: true,
dataType: 'json'
});
});
function createTableByJqueryEach(data)
{
var eTable="<table><thead><tr><th>Nome</th><th>Valor</th><th>Porque?</th><th>Data</th><th>Data de Vencimento</th><th>Produtos/Serviço</th></tr></thead><tbody>"
$.each(data,function(index, row){
eTable += "<tr>";
$.each(row,function(key,value){
eTable += "<td>"+value+"</td>";
});
eTable += "</tr>";
});
eTable +="</tbody></table>";
$('#Tabela').html(eTable);
}
My not functional php code
<?php include_once("../../../config.php");
$rows = array();
$sql= "SELECT * FROM cpg"; $result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
$rows[] = $row;
echo json_encode($rows); } ?>
Just Remove that extra closing brace:
<?php include_once("../../../config.php");
$sql= "SELECT * FROM cpg"; $result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
$rows[] = $row;
}
echo json_encode($rows); **}** ?>
What happens when you access the tabela.php file directly?
PS.. you don't/shouldn't need the $rows = array();
or $rows[] = $row;
AND, try mysqli_fetch_assoc
instead of mysqli_fetch_array
(mysqli_fetch_array will just duplicate the results, meaning you will get the column number:value
AND the column name:value
)
<?php include_once("../../../config.php");
$sql= "SELECT * FROM cpg"; $result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
echo json_encode($row);
}
?>