Try to adopt JSON in database because i have data not fixed. i can query well from terminal, and need to write same query to php script. i have spent a lot of time before ask. example:
sqlite> select json_extract(events.interni, '$') from events WHERE id='35';
output
[{"student_id":"12","student_name":"Lisa Ochoa"},{"student_id":"21","student_name":"Rafael Royal"}]
where id = 35 will become a variable of $ _POST ['id']
what I tried:
$result2 = $db->query("select json_extract(events.interni, '$') from events WHERE id='35'");
var_dump($result2->fetchAll(PDO::FETCH_ASSOC));
return [] <- empty array
i want instead = [{"student_id":"21","student_name":"Rafael Royal"}]
where did I go wrong?
I followed this answer on SO https://stackoverflow.com/a/33433552/1273715 but i need to move the query in php for an ajax call
possibile another help.
Can the result fron $ajax call can be usable as key value or remain string? in other hands i can convert string to object like students = new Object()?
eaxaple of what i need in js environment - count objects in array - and loop key value
var data = [{"student_id":"12","student_name":"Lisa Ochoa"},{"student_id":"21","student_name":"Rafael Royal"}]
consolle.log(JSON.Stringify(data));
here I would like to avoid the backslash
consolle.log(JSON.Stringify(data.lenght));
in this phase the desired data is = 2
any possible help is largely appreciated
UPDATE
leave json_extract() function i have solved the second problem, so now i can work whit object property, and finally important to count objects in array:
<?php      
        try {
            $db = new PDO('sqlite:eventi.sqlite3');
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (PDOException $e) {
        echo "I'm sorry, Dave. I'm afraid I can't do that.";
            echo $e->getMessage();
        } 
    $risultato = $db->query("SELECT * FROM events WHERE id = '35'", PDO::FETCH_ASSOC);
    $result = array();
    foreach ($risultato as $row) {
             $result[] = $row;
         }
       //  echo "Results: ", json_encode($result), "\n"; this produced backslash             
        echo $result[0]['interni'];    
    ?>
js part
var num='';
$.ajax({
  url: "sqlitedb/test-con.php", 
  type: 'POST',
  dataType: 'json',
  success:function(result){
     console.log(result[0].student_id+ " - "+ result[0].student_name); // output here is good: 12 - Lisa Ochoa
     counter(Object.keys(result).length);       
}});
function counter (numero){
  console.log("num2: =" + numero);  
}
//out put here: 2
perfect!
odd behaviour:
console.log(result[0].student_id+ " - "+ result[0].student_name);
12 - Lisa Ochoa
outup is right but
 console.log(result.lenght);
output is null