vForums Support > Programming & Coding :: Programming Discussion :: > Storing arrays & Selecting a specific column

Storing arrays & Selecting a specific column - Posted By Dylan (dylan) on 17th Feb 08 at 10:46pm
I want to generate and array of a users info when they login and store it in a session. Also how would I access this. Sofar i've got something like this.

if(!isset($_SESSION['perms'])) {
// create the array and set the perms in it.

}

but the question now is how do I access this array?

Also how do I select a specific colum from a table.

From what I understand the * is a wildcard in a mysql query so if i replaced it with the column name it would help cut back on the query. But how would I access the query afterwards would I just echo the query directly or is there a function similiar to mysql_fetch_array().

Also what does mysql_fetch_assoc() do?

Another question? What are some simple things I can do to optimize my mysql queries and databases. Heres a sample query of myne.

Code:
 
  1. $query = mysql_query("SELECT * FROM ".$prefix."_members WHERE username='$user'");
 


Thanks in advance

Re: Storing arrays & Selecting a specific column - Posted By Ross (admin) on 17th Feb 08 at 10:57pm
Quote:
Another question? What are some simple things I can do to optimize my mysql queries and databases. Heres a sample query of myne.

Code:
 
  1. $query = mysql_query("SELECT * FROM ".$prefix."_members WHERE username='$user'");
 


Thanks in advance


The first thing I would say would be to add a limit when you know how many rows you need to fetch. If you're getting a users information then you know that you only need one row and that once you have that row you can stop looking.


Code:
 
  1. $query = mysql_query("SELECT * FROM ".$prefix."_members WHERE username='$user' LIMIT 1");
 


Re: Storing arrays & Selecting a specific column - Posted By Paddy (artemis) on 17th Feb 08 at 11:31pm
Replace the * with the column name, and it will fetch only that column. {Wink}

Also, $result = mysql_query($query); will set $result to the array- which is actually not an array in this case, as there is only one value.

You may want to check out cakePHP- it makes it much easier to develop with MySQL.

~Artemis

Re: Storing arrays & Selecting a specific column - Posted By Dylan (dylan) on 17th Feb 08 at 11:42pm
Thanks both of you {Smile} But now I need to get on to the array issue, this is a pressing matter I'd liike to get figured out.


Also what is cake, I read about it on the site but still am not sure how it would work?

EDIT:
Code:
 
  1. $query ="SELECT staff FROM ".$prefix."_user_groups WHERE id='$user_level' LIMIT 1";
  2. $result =mysql_query($query);
  3. echo $result;
 


The above just returns this Resource id #26

Re: Storing arrays & Selecting a specific column - Posted By Marc (cr0w) on 18th Feb 08 at 12:40am
You still have to use mysql_fetch_array() or mysql_fetch_assoc() to grab the value. {Wink}

Code:
 
  1. $query ="SELECT staff FROM ".$prefix."_user_groups WHERE id='$user_level' LIMIT 1";
  2. $result = mysql_fetch_assoc(mysql_query($query));
  3. echo $result['staff'];
 


As for the difference between mysql_fetch_array() and mysql_fetch_assoc(), here's an example of what each would return:

mysql_fetch_array: Array(0 => "cr0w", "username" => "cr0w", 1 => "Marc", "display_name" => "Marc", 2 => 15, "age" => 15);

mysql_fetch_assoc: Array("username" => "cr0w", "display_name" => "Marc", "age" => 15);

Hope that clears it up. {Smile}

Re: Storing arrays & Selecting a specific column - Posted By Dylan (dylan) on 18th Feb 08 at 12:51am
Thanks cr0w
So I tried assoc and array and it just says Array

Re: Storing arrays & Selecting a specific column - Posted By Paddy (artemis) on 18th Feb 08 at 1:02am
Use print_r($query) to find the structure of the array. You can't 'print' an array, you need to print a certain value. So if the array is structured as $query = array('result' => array('username' => 'me'));

then print $query would yield 'Array', print $query['result'] would yield 'Array', and print $query['result']['username'] would yield 'me'.

~Artemis

Re: Storing arrays & Selecting a specific column - Posted By Marc (cr0w) on 18th Feb 08 at 1:31am
Here's just an example of how you should go about it:

Code:
 
  1. $query = mysql_query("SELECT rawr FROM blah WHERE id = '15' LIMIT 1");
  2. $return = mysql_fetch_assoc($query);
  3.  
  4. echo $return['rawr'];
 


The above code would return the contents of "rawr". {Smile}

Re: Storing arrays & Selecting a specific column - Posted By Dylan (dylan) on 18th Feb 08 at 1:35am
i couldn't print_r the query but I could print_r result and I got this

Array ( [staff] => 2 )

thats with result as an associative array

Re: Storing arrays & Selecting a specific column - Posted By Marc (cr0w) on 18th Feb 08 at 1:38am
 
i couldn't print_r the query but I could print_r result and I got this

Array ( [staff] => 2 )

thats with result as an associative array


Could you post the exact code you're using? {Smile}

Re: Storing arrays & Selecting a specific column - Posted By Paddy (artemis) on 18th Feb 08 at 2:03am
So you should be able to use print $result['staff'], and it will return '2'.

And cakePHP is a development framework. To do what you're doing in Cake, you would do $user = $this->User->find(array('username' => 'me')); print $user['User']['staff']. And that's it. ^_^

~Artemis

Re: Storing arrays & Selecting a specific column - Posted By Dylan (dylan) on 18th Feb 08 at 3:49am
Thanks guys {Smile} Arrays are my new best friend in debugging my software, makes it easy to just display all the information {Smile}