<p>What is the quickest way to get a large amount of data (think golf) and the most efficient (think performance) to get a large amount of data from a MySQL database to a session without having to continue doing what I already have:</p>

<pre><code>$sql = "SELECT * FROM users WHERE username='" . mysql_escape_string($_POST['username']) . "' AND password='" . mysql_escape_string(md5($_POST['password'])) . "'";
$result = mysql_query($sql, $link) or die("There was an error while trying to get your information.\n&lt;!--\n" . mysql_error($link) . "\n--&gt;");
if(mysql_num_rows($result) &lt; 1)
 {
    $_SESSION['username'] = $_POST['username'];
    redirect('index.php?p=signup');
 }
$_SESSION['id'] = mysql_result($result, '0', 'id');
$_SESSION['fName'] = mysql_result($result, '0', 'fName');
$_SESSION['lName'] = mysql_result($result, '0', 'lName');
...
</code></pre>

<p>And before anyone asks yes I do really need to 'SELECT </p>

<p>Edit: Yes, I am sanitizing the data, so that there can be no SQL injection, that is further up in the code.</p>

[{'answer_id': 25770, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': "<p>OK, this doesn't answer your question, but doesn't your current code leave you open to SQL Injection?</p>\n<p>I could be wrong, never worked in PHP, just saw the use of strings in the SQL and alarm bells started ringing!</p>\n<h3>Edit:</h3>\n<p>I am not trying to tamper with your post, I was correcting a spelling error, please do not roll back.</p>\n"}, {'answer_id': 25775, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 0, 'selected': False, 'text': '<p>I am not sure what you mean by "large amounts of data", but it looks to me like you are only initializing data for one user? If so, I don\'t see any reason to optimize this unless you have hundreds of columns in your database with several megabytes of data in them.</p>\n\n<p>Or, to put it differently, why do you need to optimize this? Are you having performance problems?</p>\n\n<p>What you are doing now is the straight-forward approach, and I can\'t really see any reason to do it differently unless you have some specific problems with it.</p>\n\n<p>Wrapping the user data in a user object might help some on the program structure though. Validating your input is probably also a good idea.</p>\n'}, {'answer_id': 25776, 'author': 'Marius', 'author_id': 1585, 'author_profile': 'https://Stackoverflow.com/users/1585', 'pm_score': 0, 'selected': False, 'text': "<p>Try using json for example:</p>\n\n<pre><code>$_SESSION['data'] = json_encode(mysql_fetch_array($result));\n</code></pre>\n\n<p><strong>Edit</strong>\nLater you then <code>json_decode</code> the <code>$_SESSION['data']</code> variable and you got an array with all the data you need.</p>\n\n<p><strong>Clarification:</strong></p>\n\n<p>You can use <code>json_encode</code> and <code>json_decode</code> if you want to reduce the number of lines of code you write. In the example in the question, a line of code was needed to copy each column in the database to the SESSION array. Instead of doing it with 50-75 lines of code, you could do it with 1 by <code>json_encoding</code> the entire database record into a string. This string can be stored in the SESSION variable. Later, when the user visits another page, the SESSION variable is there with the entire JSON string. If you then want to know the first name, you can use the following code:</p>\n\n<pre><code>$fname = json_decode($_SESSION['data'])['fname'];\n</code></pre>\n\n<p>This method won't be faster than a line by line copy, but it will save coding and it will be more resistant to changes in your database or code.</p>\n\n<p><strong>BTW</strong>\nDoes anyone else have trouble entering ] into markdown? I have to paste it in.</p>\n"}, {'answer_id': 25781, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p>@Unkwntech\nLooks like you are correct, but following a Google, which led <a href="http://shiflett.org/articles/sql-injection" rel="nofollow noreferrer">here</a> looks like you may want to change to <strong>mysql_real_escape_string()</strong></p>\n\n<p>As for the edit, I corrected the spelling of <em>efficient</em> as well as removed the "what is the".. Since that\'s not really required since the topic says it all.</p>\n\n<p>You can review the edit history (which nicely highlights the actual changes) by clicking the <a href="https://stackoverflow.com/revisions/25767/list">"edited a min ago"</a> text at the bottom of your question.</p>\n'}, {'answer_id': 25782, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 0, 'selected': False, 'text': "<blockquote><p>Try using json for example:</p>\n\n<pre><code>$_SESSION['data'] = json_encode(mysql_fetch_array($result));</code></pre>\n</blockquote>\n\n<p>Is the implementation of that function faster than what he is already doing?</p>\n\n<blockquote>Does anyone else have trouble entering ] into markdown? I have to paste it in</blockquote>\n\n<p>Yes, it's bugged.</p>\n"}, {'answer_id': 25787, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 0, 'selected': False, 'text': '<blockquote>@Anders - there are something like 50-75 columns.</blockquote>\n\n<p>Again, unless this is actually causing performance problems in your application I would not bother with optimizing it. If, however, performance is a problem I would consider only getting some of the data initially and lazy-loading the other columns as they are needed.</p>\n'}, {'answer_id': 25795, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 0, 'selected': False, 'text': "<blockquote>It's not so much that it causing performance problems but that I would like the code to look a bit cleaner.</blockquote>\n\n<p>Then wrap the user data in a class. Modifyng $_SESSION directly looks somewhat dirty. If you want to keep the data in a dictionary&mdash;which you can still do even if you put it in a separate class. </p>\n\n<p>You could also implement a loop that iterates over all columns, gets their names and copy the data to a map with the same key names. That way your internal variables&mdash;named by key in the dictionary&mdash;and database column names would always be the same. (This also has the downside of changing the variable name when you change the column name in the database, but this is a quite common and well-accepted trade-off.)</p>\n"}, {'answer_id': 25805, 'author': 'UnkwnTech', 'author_id': 115, 'author_profile': 'https://Stackoverflow.com/users/115', 'pm_score': 2, 'selected': True, 'text': '<p>I came up with this and it appears to work.</p>\n\n<pre><code>while($row = mysql_fetch_assoc($result))\n {\n    $_SESSION = array_merge_recursive($_SESSION, $row);\n }\n</code></pre>\n'}, {'answer_id': 25839, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 0, 'selected': False, 'text': "<p>If Unkwntech's suggestion does indeed work, I suggest you change your <code>SELECT</code> statement so that it doesn't grab everything, since your password column would be one of those fields.</p>\n\n<p>As for whether or not you need to keep this stuff in the session, I say why not? If you're going to check the DB when the user logs in (I'm assuming this would be done then, no?) anyway, you might as well store fairly non-sensitive information (like name) in the session if you plan on using that information throughout the person's visit.</p>\n"}, {'answer_id': 9407270, 'author': 'Sir', 'author_id': 1076743, 'author_profile': 'https://Stackoverflow.com/users/1076743', 'pm_score': 1, 'selected': False, 'text': '<p>Most efficient:</p>\n\n<pre><code>$get = mysql_query("SELECT * FROM table_name WHERE field_name=$something") or die(mysql_error());\n\n$_SESSION[\'data\'] = mysql_fetch_assoc($get);\n</code></pre>\n\n<p>Done.</p>\n\n<p>This is now stored in an array. So say a field is username you just do:</p>\n\n<pre><code>echo $_SESSION[\'data\'][\'username\'];\n</code></pre>\n\n<p>Data is the name of the array - username is the array field.. which holds the value for that field.</p>\n\n<p>EDIT: fixed some syntax mistakes :P but you get the idea.</p>\n'}]
['https://Stackoverflow.com/questions/25767', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']