Dylan Junior Member
Posts: 136 Status: Offline Gender: Male Joined:
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: - $query = mysql_query("SELECT * FROM ".$prefix."_members WHERE username='$user'");
Thanks in advance | |
|
|
Ross Administrator
Posts: 3,709 Status: Offline Gender: Male Age: 8 1⁄1 Joined:
Additional Groups: Support Team
pmwwwgtalkvForum | 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: - $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: - $query = mysql_query("SELECT * FROM ".$prefix."_members WHERE username='$user' LIMIT 1");
| |
|
|
Paddy Full Member
Insane Clown
Posts: 288 Status: Offline Gender: Male Location: Buffalo, New York Age: 34 Joined:
pmwwwskypeaimgtalk | 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.
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
Posts: 136 Status: Offline Gender: Male Joined:
pmwwwmsnaimxfire | Re: Storing arrays & Selecting a specific column (17th Feb 08 at 11:42pm UTC) | | Thanks both of you 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: - $query ="SELECT staff FROM ".$prefix."_user_groups WHERE id='$user_level' LIMIT 1";
- $result =mysql_query($query);
- echo $result;
The above just returns this Resource id #26 | |
|
|
Marc vChat Developer
I <3 Rossy
Posts: 3,388 Status: Offline Gender: Male Location: Ontario, Canada Age: 32 Joined:
Additional Groups: Coding Team
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.
Code: - $query ="SELECT staff FROM ".$prefix."_user_groups WHERE id='$user_level' LIMIT 1";
- $result = mysql_fetch_assoc(mysql_query($query));
- 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. | |
rroll.to— Shorten a link, rickroll your friends. |
|
Dylan Junior Member
Posts: 136 Status: Offline Gender: Male Joined:
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 | |
|
|
Paddy Full Member
Insane Clown
Posts: 288 Status: Offline Gender: Male Location: Buffalo, New York Age: 34 Joined:
pmwwwskypeaimgtalk | 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
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: - $query = mysql_query("SELECT rawr FROM blah WHERE id = '15' LIMIT 1");
- $return = mysql_fetch_assoc($query);
-
- echo $return['rawr'];
The above code would return the contents of "rawr". | |
rroll.to— Shorten a link, rickroll your friends. |
|
Dylan Junior Member
Posts: 136 Status: Offline Gender: Male Joined:
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 | |
|
|
Marc vChat Developer
I <3 Rossy
Posts: 3,388 Status: Offline Gender: Male Location: Ontario, Canada Age: 32 Joined:
Additional Groups: Coding Team
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? | |
rroll.to— Shorten a link, rickroll your friends. |
|
Paddy Full Member
Insane Clown
Posts: 288 Status: Offline Gender: Male Location: Buffalo, New York Age: 34 Joined:
pmwwwskypeaimgtalk | 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
Posts: 136 Status: Offline Gender: Male Joined:
pmwwwmsnaimxfire | Re: Storing arrays & Selecting a specific column (18th Feb 08 at 3:49am UTC) | | Thanks guys Arrays are my new best friend in debugging my software, makes it easy to just display all the information | |
|
|