1

I have a MySQL database full of user information, like their username, password, email, etc.

I want a PHP script that allows me to pull JUST their username and display it like so:

"username1","username2","username3"

Literally exactly like that, the quotes and all.

EDIT: Sorry for not supplying enough information.

The table is named "users" the field I want to pull off it is "username" I can get it to pull and display all the information, my only problem is imploding it.

2
  • man, you really need to provide the name of table, data in MySQL stored not in databases, but in form of rows in tables inside the databases. :) Commented Feb 10, 2012 at 15:37
  • No, it's not for homework, it's just something I'm doing for fun. But sorry for not supplying all the info. All I need is for you to get it to implode for me, because I can't seem to get it. Here is my $result for you to work with: $result = mysql_query("SELECT username FROM users"); Commented Feb 10, 2012 at 15:38

4 Answers 4

5

OK dude, read the comments

<?php // open a php tag

$dbc = mysql_connect("host", "username", "password"); // connect to database
mysql_select_db("db_name", $dbc) // select the database

$sql = "SELECT `username` FROM `users_table`"; // select only the username field from the table "users_table"
$result = mysql_query($sql); // process the query

$username_array = array(); // start an array

while($row = mysql_fetch_array($result)){ // cycle through each record returned
  $username_array[] = "\"".$row['username']."\""; // get the username field and add to the array above with surrounding quotes
}

$username_string = implode(",", $username_array); // implode the array to "stick together" all the usernames with a comma inbetween each

echo $username_string; // output the string to the display

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

2 Comments

Thanks a ton, I'm new to PHP, so this is all brand new to me, thanks for helping me out, and I hope to learn more!
Hey, no worries... we were all where you are once! :) I'm sure you'll be an expert in no time! Just read the comments, I've tried to be as helpful as possible. If you have any more questions ask here, or catch me on twitter @tclayson.
2

I've seen all the other answers, however have you considered using PDO instead of mysql_query functions? It's a much nicer way to work with the database.

Here's what you want to achieve in a few lines of code (using lamba functions):

 $dbh = new PDO("mysql:host=localhost;dbname=test", "yourusername", "yourpassword");
 $results = $dbh->prepare("SELECT u.username FROM users u");
 $results->execute();
 $results = $results->fetchAll();
 echo implode(", ", array_map(function(&$r) { return $r['username']; }, $results));

Output: Jamie, Bob, Chris Nice and clean. Also, you should check if you have any results that have been returned and if the query was successful.

Just another approach.

EDIT: I've just realised you're a beginner so my answer may be a bit too advanced. However, i'll leave it for others to see as a solution, and perhaps you might look into using PDO an lamba functions when you learn a bit more. Best of luck.

3 Comments

Wow, array_map... THAT's cool way to do things in PHP, thanks for reminding. :)
And I think, we all jumped to answers with whatever libraries imprinted in mind. :)
@hijarian - Yups, it's pretty funky. I use it where I can. Bear in mind: Anonymous functions can only be used from 5.3 upwards. And PDO is generally installed by default in PHP. :-)
1

Let's assume that you have a 'mydb' database and 'users' table in it.

SQL needed:

USE mydb;
SELECT username from users;

Short version:

  1. Wrap it in PHP calls to mysql PHP library
  2. Get result as an array then implode it with comma symbol.

Long version:

First we need to connect to database:

$db = mysql_connect('DATABASE_HOST', 'USER', 'PASSWORD');
if (!$db) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db('mydb', $db);
if (!$db_selected) {
    die ('Can\'t use mydb: ' . mysql_error());
}

Remember to always check the return values of functions.

Then we query the database:

$result = mysql_query('select username from users', $db);

...and fetch results in flat array (we need only usernames):

while ($row = mysql_fetch_array($result, MYSQLI_ASSOC))
{
    $data[] = $row['login'];
}

Then we format the returned data according to your specs:

$string_result = '"'. implode('", "', $data) . '"';

You can do with $string_result anything you want, just close the database connection immediately after use:

mysql_close($db);

Good luck with learning PHP, BTW. ;)

Comments

0

You could using PHP's implode, but it's probably easier just do it in SQL assuming that the list won't be too long:

SELECT GROUP_CONCAT(CONCAT('"', username, '"')) AS usernames
FROM your_table

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.