0

This is what I'm trying but I'm not sure it must be this way. I wanna charge those variables to use them in my js function later

<?php
  $id = $_GET['id'];
  $name = "";
  $lat = '';
  $lng = '';
  
$link = mysqli_connect("localhost", "root", "", "projectppy");

if (mysqli_connect_errno()) {
    printf("Falló la conexión: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT name, lat, lng FROM customers ORDER BY ID DESC";

$result = $mysqli->query($query);

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
    printf("%s (%s)\n", $row["name"], $row["lat"], $row["lng"]);
}

?>
Brenda
  • 1
  • 2
  • You already have it in a variable as an array. But you can unpack it: `[$name, $lat, $lng] = $row;`. – Markus Zeller May 06 '23 at 18:11
  • What do you mean, "charge"? To use PHP data in JS you may want to use json_encode(). But that's unrelated to mysql – Your Common Sense May 06 '23 at 18:11
  • Also, you don't seem to be getting a single row but rather array of rows. Than you need this answer https://stackoverflow.com/a/59028348/285587 – Your Common Sense May 06 '23 at 18:13
  • I just meant that I want to save it like but first I need to get the data from my database and save it in php variables – Brenda May 06 '23 at 18:47
  • Just NEVER EVER do that. Never output any data unencoded. Make it `var row = "= json_encode($row) ?>";` then use `row.name` in JS. What's so hard with it? – Your Common Sense May 06 '23 at 18:50
  • okay, but anyway something is not working on my query I just need data from a row not all. It seems like my query and fetch are wrong – Brenda May 06 '23 at 18:52
  • Hey, thank you a lot for answering , helped a lot. I'm just starting learning and now I need to learn how to use the json you said, if you can show me or something would be great anyway until now I'm already happy – Brenda May 06 '23 at 20:12

0 Answers0