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 124583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:01:24+00:00 2026-05-11T05:01:24+00:00

I want to display four (4) items’name from these id: Can I do like

  • 0

I want to display four (4) items’name from these id: Can I do like this?

SELECT item_name from items WHERE item_id IN ('001', '012', '103', '500') 

or

SELECT item_name from items WHERE item_id = '001' or item_id = '012' or item_id = '103' or item_id = '500' 

IN RESPONSE TO ALL ANSWERS

Well, most of the answers said it works, but it does not really work. Here is my code:

$query = 'SELECT `item_name` from items WHERE item_id IN('s001','a012','t103','p500')';  $result = mysql_query($query, $conn) or die (mysql_error()); $fetch =  mysql_fetch_assoc($result) or die (mysql_error()); $itemsCollected = $fetch['item_name']; echo $itemsCollected; 

The item_id is alphanumeric.

  • 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. 2026-05-11T05:01:25+00:00Added an answer on May 11, 2026 at 5:01 am

    You can do either one, but the IN query is much more efficient for this purpose for any large queries. I did some simple testing long ago that revealed it’s about 10 times faster to use the IN construct for this. If you’re asking if the syntax is correct then yes, it looks fine, other than missing semi-colons to complete the statement.

    EDIT: It looks like the actual question you were asking was "why do these queries only return one value". Well, looking at the sample code you posted, the problem is here:

    $fetch = mysql_fetch_assoc($result) or die (mysql_error()); $itemsCollected = $fetch['item_name']; echo $itemsCollected; 

    You need to loop through and iterate until there are no more results to be fetched, as Pax pointed out. See the PHP manual page for mysql_fetch_assoc:

    $sql = "SELECT item_name from items WHERE item_id IN('s001','a012')"; $result = mysql_query($sql);  if (!$result) {     echo "Could not successfully run query ($sql) from DB: " . mysql_error();     exit; }  if (mysql_num_rows($result) == 0) {     echo "No rows found, nothing to print so am exiting";     exit; }  // While a row of data exists, put that row in $row as an associative array // Note: If you're expecting just one row, no need to use a loop // Note: If you put extract($row); inside the following loop, you'll //       then create $userid, $fullname, and $userstatus while ($row = mysql_fetch_assoc($result)) {     echo $row["userid"];     echo $row["fullname"];     echo $row["userstatus"]; }  mysql_free_result($result); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want display data from database in Listbox...Here is my code, It is not
i want display 1 record from colums zodys , I'm programint in C# I
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in
In the header of the django admin, I want display a link. This link
I'd like to center a list of left-aligned items. This is what I currently
I have a list like this: <div> <ul> <li>one</li> <li>two</li> <li>three</li> <li>four</li> </ul> </div>
i want to hide time and show only the date like this(DD/MM/YYYY). I Tried
I have this table: and I want to display all friend names are without
I've got a gridview that populates from four tables, and the select command (shown
I have a simple quiz application and I want display a nice timer /

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.