2

I am fetching records as follows:

$aResult = array();
$sql     = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result  = $conn->query($sql);

while($row = mysqli_fetch_array($result)){
    $aResult['query_result'] = $row;
}

This returns only the last record in the table. I need to return all records from the sql statement.

1
  • WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST or $_GET data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Jan 9, 2017 at 5:08

3 Answers 3

5

change you $aResult['query_result'] = $row; to $aResult['query_result'][] = $row;

You've override the result each time, so you just get one.

Sign up to request clarification or add additional context in comments.

1 Comment

@KrisRoofe :) Simple and Perfect. +1 Happy Coding.
1

It seems your loop constantly overwrites the value and hence you will only ever seen the last row. I think you might see better results if you do something like:

while($row = mysqli_fetch_array($result))
{
    $aResult[] = $row;
}

so that each new row gets appended to your array

Comments

1

Try with following code, currently You are initiating the values to the same array key :

$aResult = array();
$sql     = "SELECT notes FROM table WHERE user_id = '$user_id'";
$result  = $conn->query($sql);

while($row = mysqli_fetch_array($result)){
    $aResult['query_result'][] = $row;
}

for more Detail On Array

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.