0

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".

2
  • Try changing $row[0] to $row['id'] and $row[1] to $row['statement']. Commented Jun 8, 2014 at 17:21
  • Inside the while do a print_r($row) to see if you're getting any data back and what data it is. Commented Jun 8, 2014 at 17:25

1 Answer 1

1

Seems you are returning utf-8 characters from the db.

You can set the charset to utf8 with mysqli_set_charset after you have successfully opened the connection.

mysqli_set_charset($con, "utf8");

Also you have an error in this line.

$data = mysqli_query($con, "SELECT * FROM jagharaldrig") or die(mysql_error());

Replace mysql_error with mysqli_error($con), because you are using mysqli_* functions.

$data = mysqli_query($con, "SELECT * FROM jagharaldrig") or die(mysqli_error($con));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.