silverglade Posted July 7, 2011 Share Posted July 7, 2011 Hi, I am trying to do this.When the user logs in to my site, I want to put their id from the users table into the session, but I don't know the syntax for that. Any help greatly appreciated. the table is 'users', the field is 'id' Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/ Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 What did you try? session_start Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239446 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 Â Â Â $query = "SELECT 'id' FROM users"; Â $current_id = mysql_query($query, $db); Â $_SESSION['current_id']= $current_id; Â lol. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239449 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 Look at the link I posted. Also read mysql_query so you actually understand how to use it. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239450 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 Thanks, but the first link doesn't tell me how to store database field data into a session. This is the best I could come up with. Â Â $query = sprintf("SELECT id FROM users"); Â // Perform Query $current_id = mysql_query($query); $_SESSION['current_id']=$current_id; Â please show mercy. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239452 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 am I close? thank you.  here is the new version   $query = ("SELECT id FROM users");  // Perform Query $current_id = mysql_query($query); $_SESSION['current_id']=$current_id; Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239460 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 silverglade you need to read some basic information when it's posted to you. Session variables don't work until you start the session. You have to session_start() first always.  You also did not read about mysql_query. mysql_query returns a resource to a result set, when the query succeeds. You then need to fetch the results, so no your code will not work.  I'm not going to rewrite what is already written in crystal clear fashion in examples they provide in the manual pages I linked for you. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239463 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 thanks. I knew about session_start(); it was at the top of my php page. just not with the code I posted. I will look at it again. This is the code that seems to work on the page now, unless nothing was set.  session_start(); $query = ("SELECT id FROM users");  // Perform Query $current_id = mysql_query($query); $_SESSION['current_id']=$current_id;  I did read the stuff you posted, just not carefully enough I guess. I'll have to reread.  on the mysql_query page, it has this   $query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",   mysql_real_escape_string($firstname),   mysql_real_escape_string($lastname)); // Perform Query $result = mysql_query($query);  I don't see how that is different than what I have done except for the sprintf and mysql_real_escape_string  wow board gets slow after like 1am. lol Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239465 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 Yes and right below that code there is this code:  if (!$result) {   $message = 'Invalid query: ' . mysql_error() . "\n";   $message .= 'Whole query: ' . $query;   die($message); } // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) {   echo $row['firstname'];   echo $row['lastname'];   echo $row['address'];   echo $row['age']; }  Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239467 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 whooops. sorry about that. I am getting better at reading those manual explanations though. ok is this correct? please. thanks for helping.   $query = ("SELECT id FROM users");  // Perform Query $current_id = mysql_query($query); $user_current_id = mysql_fetch_assoc($current_id) $_SESSION['current_id']=$user_current_id;   also when I go to the index page to upload photos to the database, I get the following error, I don't know what it is. any more help greatly appreciated because that might fix everything to make a user enabled photo gallery   Parse error: syntax error, unexpected T_LOGICAL_OR in /hermes/bosweb/web173/b1739/sl.brendansite1/public_html/PHOTO_SITE/gallery/index.php on line 9  and I commented next to line 9 which looks good to me.  <?php require_once('globals.php'); session_start(); $query = "SELECT image_id FROM images WHERE gallery_user = '{$_SESSION['current_id']}'";   $result = mysql_query($query, $db);   $images = array();//THIS IS LINE 9, I DON'T SEE WHAT IS WRONG, THIS PART IS FROM THE TUTORIAL.   while ($row = mysql_fetch_array($result)) {     $id = $row['image_id'];     $images[$id] = $row['filename'];   } ?> <html>   <head>     <title>Uploaded Images</title>   </head>   <body>     <div>       <h1>Uploaded Images</h1>       <p>         <a href="upload.php">Upload an image</a>       </p>      <div style="float:left;width:110px;"> <?php $i = 0; if (count($images) == 0) { ?>           <li>No uploaded images found</li>         <?php } else foreach ($images as $id => $filename) { ?>                             <table cellpadding="10px"><tr><td>           <a href="view.php?id=<?php echo $id ?>"> <img src="view.php?id=<?php echo $id ?> " width="100" height="100"/>          </a><br />           </tr></td>           </table>                     <?php if((++$i % == 0) { echo '</div><div style="float:left;width:110px;">'; } //if the current incremented value of $i, divided by 8, has a remainder of 0, write a new division.?>                       <!-- <a href="view.php?id=<?php //echo $id ?>">-->               <?php //echo htmlSpecialChars($filename) ?>                          <!-- </a>-->               <?php } ?><!--end of loop--> </div>                                <!-- <a href="view.php?id=<?php //echo $id ?>">-->               <?php //echo htmlSpecialChars($filename) ?>                          <!-- </a>-->               <?php //} ?><!--end of loop-->        </body> </html>  I don't think this code is right, it is based on code that I replaced in the tutorial  while ($row = mysql_fetch_array($result)) {     $id = $row['image_id'];     $images[$id] = $row['filename'];   } Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239469 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 I changed the line above to this  took out $db  $query = "SELECT image_id FROM images WHERE gallery_user = '{$_SESSION['current_id']}'";   $result = mysql_query($query);//took out db here  WOW remind me to go to sleep and leave the forum at about 1am eastern time. everyone leaves. unless my post was offensive and ignorant. i guess it was ignorant at least.    Ok, I have to go to bed it's 3:21 am here. thanks for helping. Derek Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239470 Share on other sites More sharing options...
Andrew777 Posted July 7, 2011 Share Posted July 7, 2011 Incase this helps, this is how I would go about it.... Â <?php session_start(); require_once('globals.php'); //I assume this just holds your db info $userid=$_SESSION['current_id']; $sql = "SELECT * FROM images WHERE gallery_user='$userid' order by DESC"; //the 'order by' is so you start with the most recent and go down. $result=mysql_fetch_array($sql); //$images = array();//THIS IS LINE 9, I DON'T SEE WHAT IS WRONG, THIS PART IS FROM THE TUTORIAL. Â Â while ($row = mysql_fetch_array($result))Â { Â Â Â Â $id = $row['image_id']; Â Â Â Â $image = $row['whater_your_picname_field_is']; echo '<img src="galleryimages/'.$image.'">'; //assuming you would keep the images in a gallery folder or whatevername echo '<br>'; Â Â } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239474 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 No.  $query = ("SELECT id FROM users");  // Perform Query $current_id = mysql_query($query); $user_current_id = mysql_fetch_assoc($current_id) $_SESSION['current_id']=$user_current_id;  The way you learn is to read the manual page for the functions you don't understand. Otherwise you will continue to blunder from mistake to mistake like this.  First a note:  You changed what was in the manual in term of variable naming for no good reason that I can see. When you do a query you get a resource back -- in this case it's a result set resource. Resource variables are special variables that exist only during runtime of a script -- things like file handles, or database connections, or a handle to a result set.  You chose to do this:  // Perform Query $current_id = mysql_query($query);  Why? Is it wrong to name your variable $current_id? You can name a variable anything you want, but why name it $current_id when it has nothing to do with the $current_id. It's a database result set. Choosing that name just made you have to come up with other bizarre variable names further in your script. Just use $result!!!!!  Your main issue however is that you have not picked up on what mysql_fetch_assoc() does. $user_current_id = mysql_fetch_assoc($current_id) $_SESSION['current_id']=$user_current_id;  So what you do here is fetch one row (you omitted any error checking of course even though the manual example showed you how to check it).  But the main problem is that mysql_fetch_assoc returns an ARRAY!!!!  In this case it's an associative array where the keys of the array match the column names from the result set.  So when you saved it into the session variable, you are saving an array, and when you try and use it later, it doesn't work in your queries, and they bomb out, causing you more confusion, and chasing your tail further.  Here's the code you SHOULD have used:  $query = "SELECT id FROM users";  // Perform Query $result = mysql_query($query); $row = mysql_fetch_assoc($result); $_SESSION['current_id'] = $row['id'];  This unfortunately is an approach that makes little sense in this context. You are doing a SELECT * FROM user, which ostensibly is a table with many users in it as time goes on. But this code, fetches the first row in the table and that is the basis driving other scripts? Something is missing.  I've corrected this so you can understand the mechanics involved. Hopefully for your own sake you'll learn enough about how these things work to be able to accomplish your goals, because cutting and pasting and attempting to modify scripts you don't understand adequately in the first place is a recipe for nothing but continued failure and flailing. This may seem harsh, but from my point of view, looking at the fact that you have 548 posts, and yet are copying and pasting what is the most basic and trivial code, while at the same time seeming to have no idea what the simple functions do or how to use them properly, just indicates to me that you're putting in no effort. You need to try harder.      Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239477 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 Thank you both for that. very much appreciated. I was happy when I got up this morning to see all of the help. This forum is awesome. Also I will try harder to teach myself, but you have to know, I am not really good at this, I am an artist, a painter, I am very right brained, and this stuff is like chinese to me even if I do have a ton of posts, it is left brained stuff..I have to stop copying and pasting code. yes. but at least I am not quitting like I wanted to yesterday. You telling me to try harder reminds me of this prick coach I used to have in swimming who thought it best to motivate me by being "honest" which was really just being rude or mean and insensitive, you don't even know who I am or what my problems in life are, or anything about me do you. Anyway, you are right about me copying and pasting code, I got impatient. Sorry I got pissed off right now, but I can only take so much from an Administrator before I need to defend myself. It's basically rude and a little too "honest", just like a cop. I t ried to be nice, but if you saw me in person, I never would have taken that crap. The ability to be anonymous brings out the worst in some people i guess.  Like this "you chose to do this" "you need to try harder"  stuff like that. know what I mean? My father is a lawyer and spent his entire life trying to be "right", just like you are now. I told him, "it isn't enough to be right, people also have to like you". He has not learned that to this day and no longer practices law, he is burned out from proving himself right to others and trying to "help" them. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239505 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 I deleted this post Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239508 Share on other sites More sharing options...
silverglade Posted July 7, 2011 Author Share Posted July 7, 2011 Hi Andrew777, I really appreciate you helping me do this. I have an error when I use that code here is the following error. Â Â Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web173/b1739/sl.brendansite1/public_html/PHOTO_SITE/gallery/index.php on line 6 Â Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web173/b1739/sl.brendansite1/public_html/PHOTO_SITE/gallery/index.php on line 10 Â I can't figure out what is wrong, mainly because the administrator dude was right, I don't know what I am doing and need to review the basics. But any more help you have until I do that for this code is greatly appreciated!. thanks. Derek. Â <?php session_start(); require_once('globals.php'); //I assume this just holds your db info $userid=$_SESSION['current_id']; $sql = "SELECT * FROM images WHERE gallery_user='$userid' order by DESC"; //the 'order by' is so you start with the most recent and go down. $result=mysql_fetch_array($sql); //$images = array();//THIS IS LINE 9, I DON'T SEE WHAT IS WRONG, THIS PART IS FROM THE TUTORIAL. Â Â while ($row = mysql_fetch_array($result))Â { Â Â Â Â $id = $row['image_id']; Â Â Â Â $image = $row['filename']; echo '<img src="galleryimages/'.$image.'">'; //assuming you would keep the images in a gallery folder or whatevername echo '<br>'; Â Â } ?> Â I don't think anyone will be helping me anymore on this thread given the response I gave to the Admin. LOL. hey I have to respect myself people, even if I am a little lazy. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239518 Share on other sites More sharing options...
PFMaBiSmAd Posted July 7, 2011 Share Posted July 7, 2011 Sorry to be blunt, but the 'fixed' code that Andrew777 posted isn't even executing the query (there's no mysql_query() statement in it.) It also isn't even doing anything that is relevant to your question of retrieving the id and storing it in a session variable and If you had been learning the meaning of each of the fundamental php statements that you have been trying to use in this thread (you don't have to learn every php statement, just the ones you are trying to use) and actually reading the code in front of you, you would have recognized this. Â The method you are currently trying to use to - "Hi, I am trying to do this", is going to take you several hundred times longer, as already demonstrated by the length of this thread, to produce any code that even comes close to working, then if you had actually gotten a basic php/mysql book and read it to pick up the fundamentals. Â Programming involves defining what you are trying to accomplish, then breaking that statement down into the steps necessary to accomplish that task, then writing and testing the code that performs each of those steps. Â Here is your statement of what you are trying to accomplish - When the user logs in to my site, I want to put their id from the users table into the session. Â Do you have code that is logging the user into your site, because typical code to do that attempts to SELECT the matching row from the user table and all that you need to do to accomplish your stated goal is to retrieve the id from that row and store it into a specifically named $_SESSION variable, provided you have a session_start() statement before outputting any characters on the page. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239568 Share on other sites More sharing options...
gizmola Posted July 7, 2011 Share Posted July 7, 2011 What I already explained in the prior post, that I guess you skimmed to the bottom of, so that you could take umbrage rather than analyzing what I was showing you, is that this script depends on the other script working right, and that script did not work right for the reasons I took great pains to explain.  Whatever family baggage you have, please don't bring that into it. You came here for help and I tried to provide it to you, but ultimately you have to help yourself. You have a funny way of looking at things, considering that I've answered thousands upon thousands of questions for people like yourself for no other reason than to try and help them learn how to program. You are right I don't know you, and I don't need to know you to go through this thread and revisit all the times that you were provided a reference to something you needed to read, only to have you follow up almost immediately with a post demonstrating you decided not to take the time.  Pasting another reply with more of the code you don't understand, along with another error message, that you wouldn't even have if you'd corrected the other problem, is exactly what I was referring to, and all you've done is proven me right. Either you want to learn how to program, and succeed at developing your system, or you don't, but trust me when I tell you that when other people seem to care more about trying to help you learn this than you do, there's something wrong with your approach. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1239571 Share on other sites More sharing options...
Psycho Posted July 8, 2011 Share Posted July 8, 2011 If I need a lawyer to defend me, a doctor to perform an operation, an accountant to do my taxes or a programmer to build a web application for me I want that person to be "right" and could care less if they are well liked. Quote Link to comment https://forums.phpfreaks.com/topic/241295-storing-field-variables-in-session/#findComment-1240102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.