0

I have one SQL query and the results are retrieved using;

$result = mysql_query($query);

And the results are printed using;

while($row = mysql_fetch_array($result)){
    echo $row['column']."<br>";
}

I want to store entire $row['column'] into an array. For that, the above code can be replaced by;

$my_array[] = "";
while($row = mysql_fetch_array($result)){
    echo $row['column']."<br>";
    $my_array[] = $row['column'];
}

But here, the while loop is executed. Is there any other efficient / better method, avoiding loops?

10
  • Why don't you just read the desired column only and you have it in the array? All you need to do is select your desired column in the query. Commented Dec 10, 2015 at 4:52
  • why -1? isnt my question clear? Commented Dec 10, 2015 at 4:53
  • @itsols is this problem related to a select query? well, I dont think so Commented Dec 10, 2015 at 4:54
  • 1
    Of course your question is clear. Why -1, even I don't know. I think there are a few people who can't negotiate between their thoughts and the mouse they use. I wish there is a system to reveal who does these down-votes. It wasn't me. Commented Dec 10, 2015 at 4:55
  • My question to you is why not just do a query like select column from table? This way, you already have it in an array. Of course it's going to be an associate array. Commented Dec 10, 2015 at 4:56

1 Answer 1

4

Store your desired value in your array right there in the first loop.

while($row = mysql_fetch_array($result)){
    echo $row['column']."<br>";
    $my_array[] = $row['column'];
}

Now you don't need 2 loops. $my_array is available to you to use anywhere you like

But here, the while loop is executed again

No, absolutely incorrect. This can be done in just 1 loop call and loop is not executed again.

But here, the while loop is executed. Is there any other efficient / better method, avoiding loops?

Nope there is none using mysql_* but yes using mysqli or pdo you can fetch all rows in an array using just one call without any loops.

How can I do that using mysqli_* can u include that in your answer

$link = mysqli_connect("localhost", "user", "password", "database");
$query = "SELECT column FROM yourTable";
$result = mysqli_query($link,$query);
$my_array = mysqli_fetch_all($result,MYSQLI_ASSOC);
Sign up to request clarification or add additional context in comments.

5 Comments

Shouldn't you include the element number to hold the value in $my_array?
No, I have only one loop, i just avoided the echo part.
@Hanky웃Panky Can I avoid that loop? Thats what i am asking
Nope you can't with mysql_*. See my last line of the edited answer
How can I do that using mysqli_* can u include that in your answer? I mean, the code

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.