Jump to content

Drawing Multiple Database Values Into Single Array Using "WHERE" Clause


msmyth23

Recommended Posts

Hey,

 

I'm having trouble getting some PHP arrays from my database.

 

This isn't exactly what is in my database, but this is a good example of what I'm trying to implement.

 

 

My database table (called 'tablename') looks like this (minus keys and ids and whatnot):

 

+----------------+------------------+                 

| users            | pagename    |

+----------------+------------------+

| user1            | page1            |

| user1            | page2            |

| user2            | page3            |

| user2            | page4            |

+-----------------+-----------------+

 

 

And my PHP looks sort of like this:

 

$user_array = mysql_fetch_array(mysql_query("SELECT pagename FROM tablename WHERE user = 'user1'"), MYSQL_NUM) or die(mysql_error());

 

 

The output I'm looking for is like this:

 

Array( [0] => 'page1', [1] => 'page2')

 

 

But I'm just getting the index 0 in the array:

 

Array([0] => 'page1')

 

 

Do you know anyway I can solve this to get all of the pagenames that have the same user into one array?

 

It's probably really simple but I've been thinking for hours and can't get it. Thanks for your help, Cheers!

 

Michael

Link to comment
Share on other sites

The mysql_fetch_* functions return one row at a time. As your query will return more than two rows you'll need to use these functions with a while loop to get all the results

$result = mysql_query("SELECT pagename FROM tablename WHERE user = 'user1'") or die(mysql_error();

// definee userPages as an array. We'll add the userpages to this array latter
$userPages = array();

// loop through the results, one row at a time
while($row = mysql_fetch_assoc($result))
{
     // add the pagename to userPages array
     $userPages[] = $row['pagename'];
}

// output the the userPages array
echo '<pre>' . print_r($userPages, true) . '</pre>';

Link to comment
Share on other sites

The mysql_fetch_* functions return one row at a time. As your query will return more than two rows you'll need to use these functions with a while loop to get all the results

$result = mysql_query("SELECT pagename FROM tablename WHERE user = 'user1'") or die(mysql_error();

// definee userPages as an array. We'll add the userpages to this array latter
$userPages = array();

// loop through the results, one row at a time
while($row = mysql_fetch_assoc($result))
{
     // add the pagename to userPages array
     $userPages[] = $row['pagename'];
}

// output the the userPages array
echo '<pre>' . print_r($userPages, true) . '</pre>';

 

 

 

It worked to perfection  ;D I knew it had to be a lot simpler than I was making it. Thanks a bunch. Cheers,

 

Michael

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.