vForums Support Banner



 

Welcome Guest! Please Login or Register
vForums Support :: Programming & Coding :: Programming Discussion :: Storing arrays & Selecting a specific column - View Topic
Topic Rating: *****
Printable View
Dylan
Junior Member
**

[Avatar]

Posts: 136
Status: Offline
Gender: Male
Joined:  
Reputation: 0%  


pmwwwmsnaimxfire
Storing arrays & Selecting a specific column (17th Feb 08 at 10:46pm UTC)
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


Image
Ross
Administrator
*****

[Avatar]

Posts: 3,709
Status: Offline
Gender: Male
Age: 8 11
Joined:  

Additional Groups:
Support Team
***


Reputation: 45%  


pmwwwtwittergtalkvForum
Re: Storing arrays & Selecting a specific column (17th Feb 08 at 10:57pm UTC)
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");
 


Image
Paddy
Full Member
***
Insane Clown

[Avatar]

Posts: 288
Status: Offline
Gender: Male
Location: Buffalo, New York
Age: 34
Joined:  
Reputation: 7%  


pmwwwtwitterskypeaimgtalk
Re: Storing arrays & Selecting a specific column (17th Feb 08 at 11:31pm UTC)
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
Dylan
Junior Member
**

[Avatar]

Posts: 136
Status: Offline
Gender: Male
Joined:  
Reputation: 0%  


pmwwwmsnaimxfire
Re: Storing arrays & Selecting a specific column (17th Feb 08 at 11:42pm UTC)
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


Image
Marc
vChat Developer
*****
I <3 Rossy

Posts: 3,388
Status: Offline
Gender: Male
Location: Ontario, Canada
Age: 32
Joined:  

Additional Groups:
Coding Team
***


Reputation: 40%  


pmwww
Re: Storing arrays & Selecting a specific column (18th Feb 08 at 12:40am UTC)
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}

rroll.to— Shorten a link, rickroll your friends.
Dylan
Junior Member
**

[Avatar]

Posts: 136
Status: Offline
Gender: Male
Joined:  
Reputation: 0%  


pmwwwmsnaimxfire
Re: Storing arrays & Selecting a specific column (18th Feb 08 at 12:51am UTC)
Thanks cr0w
So I tried assoc and array and it just says Array


Image
Paddy
Full Member
***
Insane Clown

[Avatar]

Posts: 288
Status: Offline
Gender: Male
Location: Buffalo, New York
Age: 34
Joined:  
Reputation: 7%  


pmwwwtwitterskypeaimgtalk
Re: Storing arrays & Selecting a specific column (18th Feb 08 at 1:02am UTC)
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
Marc
vChat Developer
*****
I <3 Rossy

Posts: 3,388
Status: Offline
Gender: Male
Location: Ontario, Canada
Age: 32
Joined:  

Additional Groups:
Coding Team
***


Reputation: 40%  


pmwww
Re: Storing arrays & Selecting a specific column (18th Feb 08 at 1:31am UTC)
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}

rroll.to— Shorten a link, rickroll your friends.
Dylan
Junior Member
**

[Avatar]

Posts: 136
Status: Offline
Gender: Male
Joined:  
Reputation: 0%  


pmwwwmsnaimxfire
Re: Storing arrays & Selecting a specific column (18th Feb 08 at 1:35am UTC)
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


Image
Marc
vChat Developer
*****
I <3 Rossy

Posts: 3,388
Status: Offline
Gender: Male
Location: Ontario, Canada
Age: 32
Joined:  

Additional Groups:
Coding Team
***


Reputation: 40%  


pmwww
Re: Storing arrays & Selecting a specific column (18th Feb 08 at 1:38am UTC)
 
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}

rroll.to— Shorten a link, rickroll your friends.
Paddy
Full Member
***
Insane Clown

[Avatar]

Posts: 288
Status: Offline
Gender: Male
Location: Buffalo, New York
Age: 34
Joined:  
Reputation: 7%  


pmwwwtwitterskypeaimgtalk
Re: Storing arrays & Selecting a specific column (18th Feb 08 at 2:03am UTC)
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
Dylan
Junior Member
**

[Avatar]

Posts: 136
Status: Offline
Gender: Male
Joined:  
Reputation: 0%  


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


Image
 Printable View

All times are GMT+0 :: The current time is 8:20am
Page generated in 0.3968 seconds
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums