I'm trying to use PHP to query my database. I've got it all set up and it responds on requests, the issue is that it does only return the IDs, not the statements I'm asking for. This is the response I'm getting:
{"jagharaldrig":[{"id":"1","statement":null},{"id":"2","statement":null},{"id":"3","statement":null},{"id":"4","statement":null}]}
This is my PHP-script that I use to query my database:
<?php
/*
* Script to get jagharaldrig.
* @author Simon Cedergren
*/
#Connect to Database
$con = mysqli_connect("<host>", "<user>", "<password>", "<database>");
#Check connection
if (mysqli_connect_errno()) {
echo 'Database connection error: ' . mysqli_connect_error();
exit();
}
#Ensure that the client has provided a value for "type"
if (isset($_POST["type"])){
#Setup variables
$user_id = $_POST["user_id"];
$type = $_POST["type"];
$index = $_POST["index"];
$user_id = mysqli_real_escape_string($user_id);
$type = mysqli_real_escape_string($type);
$index = mysqli_real_escape_string($index);
$data = mysqli_query($con, "SELECT * FROM jagharaldrig") or die(mysql_error());
#If no data was returned, check for any SQL errors
if ($data) {
$result_data = array();
$i=0;
while($row = mysqli_fetch_array($data)){
$result_data[$i++] = array(
'id' => $row[0],
'statement' => $row[1]
);
}
}
#Output the JSON data
echo json_encode(array('jagharaldrig' => $result_data));
}else{
echo "Could not complete query. Missing parameter";
}
?>
This is how my table looks like, and what's in it. It's just a first draft to get the querying set up. It's very basic.
MariaDB [onyktert]> SELECT * FROM jagharaldrig;
+----+-----------------------------------+
| id | statement |
+----+-----------------------------------+
| 1 | Jag har aldrig spelat schack! |
| 2 | Jag har aldrig ätit choklad! |
| 3 | Jag har aldrig varit kär! |
| 4 | Jag har aldrig åkt skridskor! |
+----+-----------------------------------+
4 rows in set (0.00 sec)
MariaDB [onyktert]> DESCRIBE jagharaldrig;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| statement | varchar(500) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
Am I missing something in my PHP-script or is this some sort of bug? I've tried querying for the statement only with
SELECT statement FROM jagharaldrig;
but then it returns 4 entries of "null".
$row[0]to$row['id']and$row[1]to$row['statement'].whiledo aprint_r($row)to see if you're getting any data back and what data it is.