1

I am new to PHP. I am trying to read MySQL with this PHP code.

....
$sql = "select * from GameMaster where UserId = '".$_POST["UserId"]."'";

$result = mysqli_query($link, $sql);

$rows = array();
$return = array();

while($row = mysqli_fetch_array($result)) {
    
    $rows['UserId'] = $row['UserId'];
    $rows['Nick'] = $row['Nick'];
    $rows['Items'] = $row['Items'];
    $rows['Skills'] = $row['Skills'];
    ...
    $rows['LastUpdate'] = $row['LastUpdate'];

    array_push($return, $rows);
}

header("Content-type:application/json");
echo json_encode($return);

Soon after runnning this code, it gets into infinite loop. When I deleted the array_push line, it did not go into infinite loop. So I guess that'd be the problem, but I can't find out why.

Is there something wrong with my code? The MySQL database is in Amazon lightsail.

Please help me. Thanks in advance for any comments.

David Kim
  • 41
  • 5
  • There's no way that can be an infinite loop. The loop is limited by the `while` condition, which has nothing to do with `array_push()`. – Barmar Jan 13 '22 at 03:12
  • Is there a reason why you need to copy everything from `$row` to `$rows`? The array keys are the same, so you can just do `array_push($return, $row);` – Barmar Jan 13 '22 at 03:15
  • I see. I will try that. Thanks :) – David Kim Jan 13 '22 at 03:53
  • It's the same. Really weird. Without array_push, it is OK , but the result format is not what I want. What could possibly cause this? – David Kim Jan 13 '22 at 04:17
  • @Barmar You are right! I found out there is an error in the next part. Silly me !!! Thanks for your time~~~~~!!!! – David Kim Jan 13 '22 at 04:54

2 Answers2

1

if you want to fetch all rows from database, and get data array of specific fields

<?php 
$sql = "select * from GameMaster where UserId = '".$_POST["UserId"]."'";
$result = mysqli_query($link, $sql);
$filtered = [];

while($row = mysqli_fetch_all($result)) {
    $filtered[] = [
        'UserId'    => $row['UserId'],
        'Nick'    => $row['Nick'],
        'Items'    => $row['Items'],
        'Items'    => $row['Items'],
        'Skills'    => $row['Skills'],
        'Items'    => $row['Items'],
        'LastUpdate'    => $row['LastUpdate'],
    ];
}

header("Content-type:application/json");
echo json_encode($filtered);
arifursdev
  • 123
  • 1
  • 6
0

foreach record of your database information you are entering new data to rows array

while($row = mysqli_fetch_array($result)) {
    // Logic of you application here

    array_push($return, $row);
}

this pushes fetched row into rows array

how ever if you are not modifying database fetched information. you can fetch information as associative array and simply store it in variable. there will be no need for loop and storing in new array

$sql = "select * from GameMaster where UserId = '".$_POST["UserId"]."'";
$result = mysqli_query($link, $sql);
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

header("Content-type:application/json");
echo json_encode($rows);
Mohsen Amani
  • 82
  • 1
  • 8