Jump to content

Creating an array


master82

Recommended Posts

I need to create an array from a list of IDs within a database table.

 

The table consists of 3 fileds, a unique ID, a user ID and the last is an int value (item number)

 

eg

1 3 7

2 4 5

3 3 2

etc...

 

How would I go about using this query

 

$query1=mysql_query("SELECT itemnumber FROM table WHERE userid = '{$user}'");

 

Would i be right in thinking I can use:

$array = mysql_fetch_array($query1);

 

to store all the item numbers associated with the user specified in the first query?

Link to comment
https://forums.phpfreaks.com/topic/54811-creating-an-array/
Share on other sites

for that you'd need to echo out the array using a foreach statement.

for example:

$sql = "SELECT ";
foreach($array as $key => $value){
     $sql .= $value . ", ";
}
$sql .= "FROM table WHERE userid = '{$user}'";

 

That's an example. It wont work completely, because you'll have a ',' just before FROM which wont work.

You'd have to check if it's the last one in the array and if so NOT output the ','

Link to comment
https://forums.phpfreaks.com/topic/54811-creating-an-array/#findComment-271084
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.