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.

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(). Commented Jan 13, 2022 at 3: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); Commented Jan 13, 2022 at 3:15
  • I see. I will try that. Thanks :) Commented Jan 13, 2022 at 3: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? Commented Jan 13, 2022 at 4:17
  • @Barmar You are right! I found out there is an error in the next part. Silly me !!! Thanks for your time~~~~~!!!! Commented Jan 13, 2022 at 4:54

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Why filter the fields when copying to the array? Just change SELECT * to SELECT UserId, Nick, ...
correct. he can write the statements to grab which fields in the sql too.
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);

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.