Hi all, I am trying to select multiple rows from the database and then separate the rows into array values so I can use them throughout my code.

This is what I have right now...

$result = mysql_query("SELECT url, image, placement FROM advert
 WHERE user='1'") or die(mysql_error());

//This grabs 3 rows with placement name equal to 'sideadtop','sideadmiddle','sideadbottom'

 ($row =  mysql_fetch_array($result, MYSQL_NUM));
$keytop = array_search('sideadtop', $row);
$sideadtop['url'] == $row[$keytop]['url'];
$sideadtop['image'] == $row[$keytop]['image'];

$keymiddle = array_search('sideadmiddle', $row);
$sideadmiddle['url'] == $row[$keymiddle]['url'];
$sideadmiddle['image'] == $row[$keymiddle]['image'];

I am trying to get the url and image values for each ad placement value. I am not sure how the output for the mysql query is sent to php. Is it sent as a multideminsional array or just a array?

Is there a better way to achieve this?

Thanks so much and have a good weekend!


SOrry if Im unclear, basically all im asking is - Is there a way to call multiple rows (3 in my case) and then sort them out according to placement variable or should I be calling individual mysql query for each row?

Thanks again

Is this a question of speed, readability, maintainability or what? You can do it either way as long as you are happy with it. There is no standard way because your database design is flawed.
Since placements for the same url are unique (you cannot have sideadmiddle more than once for the same url, or can you?) you should have fields called sideadtop, sideadmiddle etc. Then your problem would go away: you would only select one row per url.


You should use mysql_fetch_assoc instead of mysql_fetc_array
This only returns the results in the row:

$result = @mysql_query("SELECT url, image, placement FROM advert WHERE user='1' ORDER BY placement");
$i = 0;
 while($process =  @mysql_fetch_assoc($result)){
    $row[$i] = $process;
    $placement = $row[$1]['placement'];
    $$placement['url'] = $row[$1]['url'];
    $$placement['image'] = $row[$1]['image'];
so now we have the three arrays
with contents url and image
sorted by placement alpabetically ascending

hope this is helpful!!
Not quite sure what you needed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.