0
$query = "SELECT DISTINCT users FROM computers WHERE ComputerName='EN4073254W'";
while ($user_name = mysqli_fetch_array(mysqli_query($cxn, $query))) { // print out user names
    printf("<li><a href='%s'>%s</a>\n", $user_name[0], $user_name[0]);
}

I am running a query to check for distinct users based on ComputerName all of this is ran through a while loop, I did this by following an example I found here. Running this query in phpMyAdmin results in correct output, so I'm roughly confident that my query is correct. I've also spent the time chopping through this line by line in phpStorm's debugger, but alas it gives me no good insight into what is happening (it just repeats itself over and over again).

<li><a href='foo'>foo</a>
<li><a href='foo'>foo</a>
<li><a href='foo'>foo</a>
<li><a href='foo'>foo</a>
<li><a href='foo'>foo</a>
<li><a href='foo'>foo</a>
<li><a href='foo'>foo</a>
...

The desired result should be something like this

<li><a href='foo'>foo</a>
<li><a href='bar'>bar</a>
2
  • 1
    You've put the query execution in the loop condition. That's why. Commented Jun 21, 2018 at 18:12
  • Could you provide an example as an answer, please? Commented Jun 21, 2018 at 18:13

3 Answers 3

2

You are executing query in while loop so its running infinite time.

 while ($user_name = mysqli_fetch_array(mysqli_query($cxn, $query))) // runs infinite time 

change the above line to

$result = mysqli_query($cxn, $query);
while ($user_name = mysqli_fetch_array(($result,MYSQLI_NUM)))
Sign up to request clarification or add additional context in comments.

Comments

2
$query = "SELECT DISTINCT users FROM computers WHERE ComputerName='EN4073254W'";
$stmtQuery = mysqli_query($cxn, $query);
while ($user_name = mysqli_fetch_array($stmtQuery)) { 
    printf("<li><a href='%s'>%s</a>\n", $user_name[0], $user_name[0]);
}

Comments

0

Your query is fine, but you're re-executing it every time you check to see if you exit the while loop and fetching the first item.

You want something more like: $users = mysqli_fetch_array(mysqli_query($cxn, $query)) foreach ($users as $user_name) { ... }

1 Comment

This sort of works, but spits out double of everything and it seems to cause my server to hang.

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.