1

I am completely new in PHP and MySQL but I need to make quick project for tomorrow. I do not know exactly where to find a solution for my problem because I do not know enough about this languages.

I will be glad if somebody will help me and find why I get blank page after that code:

<?php
$servername = "xyz.xyz";
$username = "123";
$password = "123";
$dbname = "123";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT `id`,`symbol`,`shortcut` FROM `Table`";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    $currencyArray = array();

    while($row = $result->fetch_assoc()) {
        $currency = array($row["id"], $row["symbol"], $row["shortcut"]);
        $currencyArray[] = $currency;
    }

    print json_encode($currencyArray);
} else {
    echo "0 results";
}
$conn->close();
?>

I want to display all data from the Table in JSON as an array with arrays that contains all data. Right now I have blank page. I will be glad for help.

Dzeremix
  • 446
  • 1
  • 5
  • 24
  • You need to turn on error reporting so you can see what's not working and the location of the error. – Sloan Thrasher May 08 '17 at 21:33
  • I am not sure how to do this unfortunately. If I think correctly: I tried print `$currency` and it works but adding this object to `$currencyArray` fail from some reason. – Dzeremix May 08 '17 at 21:51
  • http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – Barmar May 08 '17 at 21:54
  • Your code looks like it should work. `print $currency` shouldn't work, since you can't print an array; you should use `print_r($currency);` – Barmar May 08 '17 at 21:57
  • When I add `print json_encode($currency);` it shows me data. There is while loop `while($row = $result->fetch_assoc()) { $currency = array($row["currency-id"], $row["currency-symbol"], $row["currency-shortcut"]); $currencyArray[] = $currency; print json_encode($currency); }` – Dzeremix May 08 '17 at 22:02
  • I found the problem but I do not know how to fix it. In the table one of the record had polish letter "ł" and that was the reason why I can not display JSON. I had added it manually by the MySQL panel. I do not know how to fix it :( – Dzeremix May 08 '17 at 22:21
  • `json_encode($result->fetch_all(MYSQLI_ASSOC), JSON_UNESCAPED_UNICODE);` – E_p May 08 '17 at 22:24
  • Wich encoding are you using in your table? ISO? UTF-8? You need to specify in your connection with database with mysqli->set_charset function. Anyway, you can convert any string to UTF-8 encoding using mb_convert_encoding function :) – thejoin May 09 '17 at 11:03

1 Answers1

2

Try to enable error_reporting in you php settings. Maybe the json_encode function is returning an error and, according to the php doc, is returning a false. That's why you see a blank page.

You can use the json_last_error function to see what kind of error is (doc).

Maybe is an encoding issue? A json is encoding in UTF-8. Try to debug your code, because we don't what kind of error is returning or to know how your data are.

I suggest you to put some headers if you print out a json string:

header("Content-type: application/json;charset=utf-8");
echo json_encode($array);
thejoin
  • 326
  • 2
  • 8