0

I'm trying to retrieve all the rows from my MySQL database through a PHP script. I'm pretty sure connecting to the database went well, but the code that needs to be run afterwards doesn't get executed (or so it seems). Even if the script couldn't return any data, it should at least return an empty array, but when I open the file, it shows an empty page with no text at all. What am I doing wrong?

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve all rows from the database
$sql = "SELECT * FROM u8338p5759_iosdb.Posts";
$result = $conn->query($sql);

$dataArray = Array();

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    // Assign each row into an array
    $dataArray[] = $row;

}
echo json_encode($dataArray);

} else {
    echo json_encode($dataArray);
}

// Close the connection
$conn->close();
7
  • First change your $row * $result->fetch_assoc() to $row = $result->fetch_assoc() and make sure $conn is established. Commented Sep 6, 2016 at 18:40
  • How stupid of me! I actually had the correct code running perfectly fine a while back, and when I tried to move everything to a new FTP server, I had to retype the code by hand (copy/pase didn't work), and I accidentally put a "*" where I needed a "=". I've been going through my code character by character to see what went wrong, but I missed this little mistake every single time. Thanks for noticing and my apologies for my clumsiness! :) Commented Sep 6, 2016 at 22:52
  • @AnowarHossainJeebon By the way, I just added the connection code to my post, so you can see if I did that right. I double-checked to make sure all the variables in the mysqli parameters are matching my database credentials. Commented Sep 6, 2016 at 23:08
  • yes now it's okay as you say your database credential is okay. I have checked in my local by: $conn = new mysqli('localhost', 'root', '', 'test'); Full code is working. Nice :) Commented Sep 6, 2016 at 23:38
  • @AnowarHossainJeebon So does it actually work for you? Are you seeing the JSON array? When I try to view the page in my web browser I just get a blank page. I can't find any errors in the error log and all my credentials are matching. This is so frustrating. Do you have any experience with fixing this kind of problem? Commented Sep 7, 2016 at 18:02

1 Answer 1

1
while($row * $result->fetch_assoc()) {
           ^----

Exactly what do you think this multiplication operation should be producing? "undefined variable times array" isn't exactly a useful operation.

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.