-3

I have an doubt about json, I am fetching value from database by using json array!! i.e: updated:

$json=[];
$query="select * from json where id='".$id."'";
$run=mysqli_query(connection,$query);
$i=0;
while($exe=mysqli_fetch_array($run,MYSQLI_ASSOC)) {
    $json[$i]["name"]= $exe["name"];

}

note: this code is in another page(page 1) and i get values through a ajax call in another page(page 2) and i need to access value of $json[$i]["name"]= $exe["name"]; as $name in page 2 for another function ex : $query1= "select name from json1 where name='".$name.'""; i dont know how to access value in json array.please help me to clear this doubt

Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
  • Your question is lacking content, please read [this](https://stackoverflow.com/help/how-to-ask). Then edit your question, to make it more clear for us to help you. – Periklis Kakarakidis Nov 16 '21 at 11:28
  • @PeriklisKakarakidis i think now you can get it what i am trying to say! – Thasleem Liyazee Nov 16 '21 at 11:40
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 17 '21 at 10:20
  • how to use parameterized prepared statements? @Dharman – Thasleem Liyazee Nov 17 '21 at 10:26
  • Read https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – Dharman Nov 17 '21 at 10:26
  • If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection – Dharman Nov 17 '21 at 10:26

1 Answers1

0

Page 1:

<?php 
$json=[]; 
$query="select * from json where id='".$id."'"; 
$run=mysqli_query(connection,$query); 
$i=0; 
while($exe=mysqli_fetch_array($run,MYSQLI_ASSOC)){ 
    $json[$i]["name"]= $exe["name"];
}
echo json_encode($json);

I'm assuming that this js function will be called when data is received through AJAX:

function success(data_in_string) {
    var js_obj = JSON.parse(data_in_string);
    console.log(js_obj[0]["name"]);
}
Ammar Aslam
  • 560
  • 2
  • 16