0

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 .

1
  • Don't use mysql_fetch_array(), use mysql_fetch_assoc() instead, that way you can fetch the single entries one by one in a while loop. This is shown in millions of examples out there on the net. Plus it scales better, since it processes sequencially instead of loading all entries into RAM, that might be thousands of entries. That said: the old mysql extension has been deprecated log ago, this is documented, you should switch to either the newer mysqli extension or PDO and make use of prepared statements. Commented Jan 17, 2014 at 16:33

2 Answers 2

1

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"];
}
Sign up to request clarification or add additional context in comments.

7 Comments

Or (if this is what OP wants) make use of the new array_column() function in PHP 5.5
@user3078441 What you do get when you print_r($row); inside the foreach?
@user3078441 Then you have a problem somewhere else. What about print_r($rows); before the foreach? If nothing, try var_dump($results); and see what you get for that. Do some basic debugging.
@Patric Q:Sorry I missed "s" in rows=mysql_fetch_assoc ... print_r gives me "hello1234" , NAME column for id = 1 is "hello" and DESCRIPTION for id = 1 is "1234" ... echo $names[0] gives "h" and echo $names[1] gives "1" ... $names[1] gives nothing ! I wonder why two column NAME and DESCRIPTION are assigned in $names !
@user3078441 See my updated answer. Remove your current use of $rows = mysql_fetch_arry($result, MYSQL_ASSOC); and use what I have instead. While doing this, keep in mind that you really shouldn't be using the mysql_* functions at all, as they have been deprecated.
|
0

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

2 Comments

Some of use would be happy to be able to use 5.4, let alone 5.5 :)
@PatrickQ - PHP 5.6 is almost ready for alpha1 release.... I guess I'm lucky in my professional environment because I can decide for myself which version of PHP I want to use... sadly I'm still forced to retain 5.2 compatibility for my Open Source work :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.