Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p><strong>EDIT</strong>: 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: </p> <pre><code>$fetch = mysql_fetch_assoc($result) or die (mysql_error()); $itemsCollected = $fetch['item_name']; echo $itemsCollected; </code></pre> <p>You need to loop through and iterate until there are no more results to be fetched, as <a href="https://stackoverflow.com/questions/544750/how-to-select-multiple-records-row-from-a-column-in-mysql/544757#544757">Pax pointed out</a>. See the PHP manual page for <a href="http://us3.php.net/mysql_fetch_assoc" rel="nofollow noreferrer"><strong>mysql_fetch_assoc</strong></a>:</p> <pre><code>$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); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload