Call mysql data of specific tablerow into hype

I didn’t say you mustn’t … just that I have a different mentality. If it practical is another discussion. I can offer help but only in a couple of hours, though. On the phone and the road. As usual.

Btw: at this point it’s implemention only. The solution and combined effort still rock :blush:

@MaxZieb vielleicht besser in deutsch :wink: das problem ist, dass ich daten von der datenbank mittlerweile laden kann, hineinschreiben kann, … funktioniert alles. das einzige problem ist dieses auslesen einzelner zellen.
hast du da mehr erfahrung damit?

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

4 Likes

@DBear - thank you very much - DANKESCHÖN!!
it´s working now as expected.
the problem was the php - you wrote first to create an array:

$output = mysql_fetch_array($result);

then you changed it to

$output = mysqli_fetch_assoc($result);

… and this is working.
this was the missing link and again thanks for your help.

1 Like

Hello,

I'm trying to get the script to work. I'm trying to load a wordpress database in HYPE. But It doesn't seem to work. I get an 500 (internal server error). What am I doing wrong? I'm using jquery-2.1.3.js and use a rectangle with ID 'result' and a 'text' with ID 'output'.

PHP code:

<?php $servername = "localhost"; $username = "***"; $password = "***"; $dbname = "***"; $tablename = "wp_posts";

// 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=1";
$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: "get.php",

type: "POST",
success: function(data){
console.log(data);
var arr = JSON.parse(data);
result.innerHTML = arr["post_content"] + ", " + arr["post_title"];
output.innerHTML = JSON.stringify(arr);
}
});

I recommend trying to get the PHP side of things working before working with PHP + Hype. Make sure the permissions on your PHP file are correct and that you can load your post_content within a regular (bare bones) HTML page first. A 500 error usually means that the server doesn't know how to handle the php code or the php file itself. You may see more info if your host exposes PHP logs.

Hello Daniel,

Thanks for the reply. I first tested it in a HTML page before I test it with Hype. The HTML page works but in Hype it doesn't so I came to this post and tried the solution above.

I've changed the PHP script as I had in the HTML Page and now I get an error 'uncaught syntaxerror unexpected end of json input at json.parse ( anonymous )'. It has to do with the JSON in the PHP script. I think you understand I'm not quit good at this.

PHP file:

<?php $servername = "localhost"; $username = "***"; $password = "***"; $dbname = "***"; // $tablename = "wp_posts";

// 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 wp_posts";
$query = mysqli_query($conn, $sql);

if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>

Hype:

var result = hypeDocument.getElementById('result')
var output = hypeDocument.getElementById('output')

$.ajax({
    url: "get2.php",

type: "POST",
success: function(data){
console.log(data);
var arr = JSON.parse(data);
result.innerHTML = arr["post_content"] + ", " + arr["post_title"];
output.innerHTML = JSON.stringify(arr);
}
});