I am a bit lost with my PHP file. I want to find an array with all my datas in the database, and send it with ajax to my js file (this part is OK).
Now, i have :
$cnx = new PDO('mysql:host=localhost;dbname=simul','root','');
$cnx->query('SET NAMES utf8');
$cnx->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$req = $cnx->prepare('Select * from price');
$req-> execute();
$data = $req->fetchAll(PDO::FETCH_KEY_PAIR);
print_r($data);
My JS :
function set_global_var(){
$.ajax({
type: "POST",
url: '/global.php',
async: false,
success: function(rep){console.log(rep);},
});
}
Print_r :
Array
(
[0] => Array
(
[small-bronze] => 5
)
[1] => Array
(
[small-silver] => 10
)
)
And to access it fast, I just want something like :
[small-bronze] => 5
[small-silver] => 10
[small-gold] => 15
Data Base :
item price
small-bronze 5
How can I do this efficiently ? Is PDO::FETCH_KEY_PAIR wrong ? Thank you in advance !