bradblog2 Posted June 18, 2008 Share Posted June 18, 2008 Hey there everyone. I have a little coding project that I am working on and cannot get the PHP part working. Basically all I want to do is get the information (supplied by the user at the time of registration) to display when the server calls for it in their profile page. Here is my code. Thanks <?php $link=mysql_connect("localhost","root","lolfunny1"); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db=mysql_select_db("members"); if(!$db) { die("Unable to select database"); } $result = mysql_query("SELECT * from members WHERE member_id='$_SESSION['SESS_MEMBER_ID']'"); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } $row = mysql_fetch_row($result); ?> Any suggestions? I'm very N00B at this and could use any tips. Brad Quote Link to comment Share on other sites More sharing options...
whizard Posted June 19, 2008 Share Posted June 19, 2008 Try $result = mysql_query("SELECT * from members WHERE member_id='" . $_SESSION['SESS_MEMBER_ID'] . "'"); HTH Dan Quote Link to comment Share on other sites More sharing options...
Wolphie Posted June 19, 2008 Share Posted June 19, 2008 You shouldn't really post your database connect details on forums perhaps a moderator will remove it. Anyway, back to the point: <?php if($con = mysql_connect('servername', 'username', 'password')) mysql_select_db('db_name') or die('MySQL Error: ' . mysql_error()); if(isset($_SESSION['id'])) { // make sure the session is set if(is_int($_SESSION['id'])) $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database! $query = sprintf("SELECT * FROM `db_name` . `table_name` WHERE `id` = '%d' LIMIT 1", $uid); // Make sure it only calls one user $result = mysql_query($query) or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_array($result)) { // while loop if(mysql_num_rows($result) > 0) { print $row['username']; // etc... print $row['email']; // etc... } else print 'User does not exist!'; } } else die 'You are not logged in!'; mysql_close($con); ?> That's how I would do it personally, but your query should be: $result = mysql_query("SELECT * from members WHERE member_id=' . $_SESSION['SESS_MEMBER_ID'] . '"); EDIT: whizard got there before me =[ Quote Link to comment Share on other sites More sharing options...
bradblog2 Posted June 19, 2008 Author Share Posted June 19, 2008 Alright, I tried replacing the code with the example above and I got the following error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in xxxxxxxxxx on line 11 I tried the second set of code (replacing the first one) and simply nothing displayed. Is there something I'm doing wrong? Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Try using: <?php $con = mysql_connect('servername', 'username', 'password') if($con) { mysql_select_db('db_name') or die('MySQL Error: ' . mysql_error()); } if(isset($_SESSION['id'])) { // make sure the session is set if(is_int($_SESSION['id'])) { $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database! $query = "SELECT * FROM table_name WHERE id = '".$uid."' LIMIT 1"; // Make sure it only calls one user $result = mysql_query($query) or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_array($result)) { // while loop if(mysql_num_rows($result) > 0) { print $row['username']; // etc... print $row['email']; // etc... } else { print 'User does not exist!'; } } } } else die 'You are not logged in!'; mysql_close($con); ?> I edited Wolphies code a little Quote Link to comment Share on other sites More sharing options...
abdfahim Posted June 19, 2008 Share Posted June 19, 2008 I don't get how possibly could you use session variables in a page where you didn't start session. On top of the page, you should write if (!isset($_SESSION)) { session_start(); } Quote Link to comment Share on other sites More sharing options...
bradblog2 Posted June 19, 2008 Author Share Posted June 19, 2008 @ Stehpen - Parse error: syntax error, unexpected T_IF in xxxx on line 4 @ abdbuet - I have a login script that starts a session when they log in. Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Sorry, forgot to add a linebreak. <?php $con = mysql_connect('servername', 'username', 'password'); if($con) { mysql_select_db('db_name') or die('MySQL Error: ' . mysql_error()); } if(isset($_SESSION['id'])) { // make sure the session is set if(is_int($_SESSION['id'])) { $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database! $query = "SELECT * FROM table_name WHERE id = '".$uid."' LIMIT 1"; // Make sure it only calls one user $result = mysql_query($query) or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_array($result)) { // while loop if(mysql_num_rows($result) > 0) { print $row['username']; // etc... print $row['email']; // etc... } else { print 'User does not exist!'; } } } } else die 'You are not logged in!'; mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
abdfahim Posted June 19, 2008 Share Posted June 19, 2008 @ abdbuet - I have a login script that starts a session when they log in. As of my knowledge, you have to start your session in every page to keep the session variables. It is not enough that you start it during one logs in and then don't start it in other pages. Quote Link to comment Share on other sites More sharing options...
bradblog2 Posted June 19, 2008 Author Share Posted June 19, 2008 @abduet - Alright I'll add that in. Quote Link to comment Share on other sites More sharing options...
bradblog2 Posted June 19, 2008 Author Share Posted June 19, 2008 Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in xxxxx on line 26 Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 19, 2008 Share Posted June 19, 2008 Hmm, try this: <?php $con = mysql_connect('servername', 'username', 'password'); if($con) { mysql_select_db('db_name') or die('MySQL Error: ' . mysql_error()); } if(isset($_SESSION['id'])) { // make sure the session is set if(is_int($_SESSION['id'])) { $uid = mysql_real_escape_string(htmlentities(htmlspecialchars($_SESSION['id']))); // secure the data for inserting into the database! $query = "SELECT * FROM table_name WHERE id = '".$uid."' LIMIT 1"; // Make sure it only calls one user $result = mysql_query($query) or die('MySQL Error: ' . mysql_error()); while($row = mysql_fetch_array($result)) { // while loop if(mysql_num_rows($result) > 0) { print $row['username']; // etc... print $row['email']; // etc... } else { print 'User does not exist!'; } } } } else { die("You are not logged in!"); } mysql_close($con); ?> Otherwise it looks like it'd work to me. Quote Link to comment Share on other sites More sharing options...
bradblog2 Posted June 19, 2008 Author Share Posted June 19, 2008 Thanks for your help. Stephen and everyone. I really appreciate it. It now says I'm not logged in, which is a problem with the other code. At least it is that far Quote Link to comment 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.