1

I have previously constructed an array manually, e.g.:

<?php
$noupload = array('nick', 'cliff');
?>

but now I am trying to populate the array automatically from users in a MySQL database. So far I have this:

<?php
// Make a MySQL Connection
$debug=false;
$conn = mysql_connect("host","user","password"); // your MySQL connection data
$db = mysql_select_db("database");

$query = "SELECT * FROM users WHERE access LIKE '%listen%'"; 

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    $listen = "'". $row['login']. "', ";
}
?>

$listen constructs the user list in the way that I previously entered it manually (I tested this using echo), but I am not sure how to pass this to $noupload. I tried:

$noupload = array($listen);

but this didn't work. I think I'm close and would be grateful for some help over the final hurdle,

Thanks,

Nick

3
  • To confirm - you may get multiple results from your SQL query and you want to create a string array with the 'login' column from each result? Commented Apr 30, 2011 at 19:52
  • 1
    Also, why have a SELECT * if you're only accessing the login attribute, you can SELECT login FROM users WHERE access like "%listen%". If you don't need more data then the login field, don't ask the DB for more. Commented Apr 30, 2011 at 20:25
  • Thanks, I have set it to SELECT login only. Commented Apr 30, 2011 at 20:29

1 Answer 1

1

You can declare $listen as an array

$listen = array();
while ($row = mysql_fetch_array($result)) {
    $listen[] = "'". $row['login']. "'";
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, that worked great! I will mark as the answer in a couple of minutes
I can see you have now edited the line $listen[] = "'". $row['login']. "'"; It worked before you edited it. Should I change it?
As a side note, use $listen[] = "'{$row['login']}'"; It saves two unnecessary string concatenations. (. operator)
This makes an array but from your example I think you just want $listen[] = $row['login']; ie drop the quotes. It seems you were only adding the quotes to simulate the syntax of declaring an array.
Yes, the edited version above doesn't work, but the original did work, with the line $listen[] = $row['login'];.
|

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.