Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:17:24+00:00 2026-05-23T14:17:24+00:00

Question : How to display 15 random usernames with single quote on each username

  • 0

Question : How to display 15 random usernames with single quote on each username in an array?

Code :

$friendsArray = array("zac1987", "peter", "micellelimmeizheng1152013142",.....);
$randomfriends = array_rand($friendsArray, 15); //random select 15 usernames

foreach($randomfriends as $c => $friend){ //wrap each username in a single quote
    $friendsArray[$c] = "'".mysql_real_escape_string($friendsArray[$c])."'";
}

$friendsArray2 = join(', ',$friendsArray);
echo $friendsArray2;

Output :

'hh', 'gg', 'ff', 'dd', 'ss', 'aa', 'oo', 'ii', 'uu', 'yy', 'tt', 'rr', 'ee', 'ww', 'qq', micellelimmeizheng1152013142, vv, bb

The problem can be seen obviously through the output. micellelimmeizheng1152013142, vv, bb are 16th, 17th and 18th entry without single quotes, they supposedly not to be shown, how to delete them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:17:25+00:00Added an answer on May 23, 2026 at 2:17 pm

    Your issue is that you are not reseting $friendsArray and merely overwriting certain keys with:

    $friendsArray[$c] = "'".mysql_real_escape_string($friendsArray[$c])."'";
    

    Here’s your complete, corrected code, borrowing from zerkms:

    shuffle($friendsArray);
    $rand = array_slice($friendsArray, 0, 15);
    
    foreach($rand as $friend) {
        $sql_output[] = "'".mysql_real_escape_string($friend)."'";
    }
    
    $sql_output = join(', ',$sql_output);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup on the question: ASP.NET next/previous buttons to display single row
In this question someone asked for ways to display disk usage in Linux. I'd
This question deals with concurrency issues, for suggestions on how to display a busy
As the question says, it just escaped my memory how to display xml in
This is a UI paradigm question I think. How do you display Child information?
I'm building text balloons to display variable length messages in Flash. My question is
hey, my question is that i want to display the names whose payment type
Applogies for what I have no doubt is a noob question. I display several
More a question about CSS, than about jQuery. I display top 7 players and
I'm making a flash quiz which will have a series of questions. Each question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.