Autsch! Ich verstehe auch deutsch. Ich schreibe nicht sehr gut. Also werde ich auf Englisch schreiben.
The code I have written is sound. It should echo out an array which you can then parse. I’m not sure why it’s not doing it for you.
Here is a demo for you. (This is my own server and a newly created database)
Here are 2 other ways you could do it.
Associative array:
$result = mysqli_query($conn, $sql);
$output = mysqli_fetch_assoc($result);
echo json_encode($output);
Demo here
Or
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo json_encode($row);
}
}
Both should output an associative array that looks like {“id”: “1”, “firstname”: “John”, etc …}
and you can access the array in Hype by parsing it and using the convention array["key"]
;
Here is the full code I’ve used
PHP file:
<?php
$servername = "localhost";
$username = "************";
$password = "***********";
$dbname = "cl17-dbear";
$tablename = "MyGuests";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//select info from table
$sql = "SELECT * FROM $tablename WHERE id=2";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo json_encode($row);
}
mysqli_close($conn);
?>
Hype function:
var result = hypeDocument.getElementById('result')
var output = hypeDocument.getElementById('output')
$.ajax({
url: "conn.php",
type: "POST",
success: function(data){
console.log(data);
var arr = JSON.parse(data);
result.innerHTML = arr["firstname"] + ", " + arr["lastname"];
output.innerHTML = JSON.stringify(arr);
}
});
Demo
Oh and one thing to add. If you want to update more than 1 element with the same info just use a symbol / persistent symbol.
And that will be all the help I’m giving as clearly I haven’t got the experience as per the quote by strmiska above. Also I’m away for 3 weeks so I won’t have time to help.
Cheers