I've got two tables in my DB MySQL:
Table predictiongame_bet
(uid, mid, bet_home, bet_away)
Table users
(uid, name)
This is my query:
SELECT U.name AS name, PB.bet_home as casa, PB.bet_away as trasferta
FROM predictiongame_bet as PB
JOIN users as U ON U.uid = PB.uid
WHERE mid=49
ORDER BY U.name ASC
On PhpMyAdmin it works correctly and I get my view, but I'm trying to get those values in PHP and with an AJAX POST call where I pass the SQL query.
This is my code:
function cercaRisultati() {
// value = $('#inputValue').val();
// tabella = 'predictiongame_bet';
// columnCondition = "mid = "+value;
sql = "SELECT U.name AS name, PB.bet_home as casa, PB.bet_away as trasferta
FROM predictiongame_bet as PB
JOIN users as U ON U.uid = PB.uid
WHERE mid=49 ORDER BY U.name ASC";
console.log('SQL: '+sql);
$('#sqlArrayUtenti').val(sql);
return tabella;
}
$(document).ready(function(){
$('#buttonCercaDB').on("click",function(){
var sql=$('#sqlArrayUtenti').val();
$.ajax({
url:'cerca-risultati.php',
method:'POST',
data:{
sql:sql,
},
dataType: 'json',
success:function(response){
response.map(el=>{
console.log(el);
});
$("#sqlAssegnazionePunti").css('display','block');
}
});
});
});
That is the cercarisultati.php code:
$sql = $_POST['sql'];
$i = 0;
$arr_match = array();
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr_match[$i]['name'] = $row['name'];
$arr_match[$i]['bet_home'] = $row['casa'];
$arr_match[$i]['bet_away'] = $row['trasferta'];
$i++;
}
echo json_encode($arr_match);
}
else { echo "0 results"; }
It seems that $row['name']
is always null, why?
If I remove from the while loop $arr_match[$i]['name'] = $row['name'];
it works ok, but, of course, it is not what I want.