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.
Thanks in advance
Another question? What are some simple things I can do to optimize my mysql queries and databases. Heres a sample query of myne.
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");
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
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
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.
Thanks cr0w
So I tried assoc and array and it just says Array
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
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".
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
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?
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
Thanks guys Arrays are my new best friend in debugging my software, makes it easy to just display all the information