0

I looked at this question: Create PHP array from MySQL column and what seems to work for everyone is this:

$array= array();
    while ($row = mysql_fetch_array(mysql_query("SELECT Username FROM inloggen"))) {
    $array[] = $row['Username'];
    }

But when I run this code it infinitely adds the first username in my database to the array. Does anyone know what I'm doing wrong?

1
  • 1
    mysql_query is an obsolete interface and should not be used in new applications and will be removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices. Commented Apr 14, 2014 at 21:13

1 Answer 1

1

You're re-executing the query endlessly, because you're doing it as part of your while, so if any records are returned , your code will re-query and return the same result time and again

Execute the query, then iterate over the result set

$result = mysql_query("SELECT Username FROM inloggen");
$array = array();
while ($row = mysql_fetch_array($result)) {
    $array[] = $row['Username'];
}

Caveat: The MySQL extension is a deprecated interface; you should be using MySQLi or PDO

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.