简体   繁体   中英

Assigning multiple variables with multiple column in php

I use:

foreach($image_files as $index=>$file)} 

to loop between pictures in a directory and list them in a webpage, every picture has three related column in the database (ID, NAME, DESCRIPTION) ... so I used

SELECT `NAME`, `DESCRIPTION` FROM `pic_info` WHERE `ID`= '$counter' ... 

but i don't know how can I split the

$rows = mysql_fetch_array($result, MYSQL_ASSOC) 

to 2 variable for each picture ... for example : for the first pic I need

$name[0] = pic1 and $description[0] = blahblahblah1 ... 
$name[1] = pic2 , $description[1] = blahblahblah2 ... 

using

foreach ($rows as $key => $value) {$$key = $value; } 

just gives me a combined data like $name[0] and $description[0] that I don't know how can I split it to two variable ... and it'll be appreciated if someone show another ways to SELECT two column in a table and assign it to two variables .

It's a bit unclear what you're asking, but I think you might want something like this

$names = array();
$descriptions = array();

while($row = mysql_fetch_assoc($results))
{
    $names[] = $row["NAME"];
    $descriptions[] = $row["DESCRIPTION"];
}

Requires PHP >=5.5, but this might achieve what you're after

$names = array_column($rows, 'NAME');
$descriptions = array_column($rows, 'DESCRIPTION');

or simply build the arrays you want while looping through the resultset of your SQL query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM