2

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.

8
  • You need to turn on error reporting so you can see what's not working and the location of the error. Commented May 8, 2017 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. Commented May 8, 2017 at 21:51
  • stackoverflow.com/questions/845021/… Commented May 8, 2017 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); Commented May 8, 2017 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); } Commented May 8, 2017 at 22:02

1 Answer 1

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);
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.