akirablaid Posted February 23, 2009 Share Posted February 23, 2009 Hey guys, this is my first post and I hope your knowledge can help, I'd like to use this site as a resource. I'm trying to put together a members' page for logged in users in a script I'm building. I want to call a function, which grabs all the userdata from the sql database. This should enable me to write on the members page using handy variables like $u_name and $u_id. Here's the function I'm calling: function ui($user) { $match = "select * from users where username = '".$user."'"; $result = mysql_query($match) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($result); if ($num_rows <= 0) { echo "Sorry, info for this user not found."; } else { $qry = mysql_fetch_array($result); $ui_un = $qry[username]; $ui_id = $qry[id]; $ui_dname = $qry[dname]; $ui_fname = $qry[fname]; $ui_lname = $qry[lname]; } } And so I call this with ui($mysite_username); and then attempt to print users information. It simply does not work. Weather I use $ui_un or $qry[username], the variables simply do not carry over from the function (which is in the config file). So, how can I get these variables to carry through? Regards, Akira Quote Link to comment https://forums.phpfreaks.com/topic/146574-issues-carrying-over-variable/ Share on other sites More sharing options...
jackpf Posted February 23, 2009 Share Posted February 23, 2009 Hey, try this. I just neatened it up a bit. If this doesn't work, if you could post an error message/what it's doing wrong would be useful. function ui($user) { $result = mysql_query("SELECT * FROM users where username = '$user'") or die ("Could not match data because ".mysql_error()); if(mysql_num_rows($sql) == 0) { echo "Sorry, info for this user not found."; } else { $qry = mysql_fetch_array($result); $ui_un = $qry['username']; $ui_id = $qry['id']; $ui_dname = $qry['dname']; $ui_fname = $qry['fname']; $ui_lname = $qry['lname']; } } Quote Link to comment https://forums.phpfreaks.com/topic/146574-issues-carrying-over-variable/#findComment-769507 Share on other sites More sharing options...
Goafer Posted February 23, 2009 Share Posted February 23, 2009 can't remember if it actaully matter of not, but better syntax would be: Instead of $qry[username]; use $qry['username']; <-- notice the quotes? may help if that doesn't work then i'd suggest that your database isn't being accessed correctly, check your connectiion information Quote Link to comment https://forums.phpfreaks.com/topic/146574-issues-carrying-over-variable/#findComment-769521 Share on other sites More sharing options...
Technocracy Posted February 23, 2009 Share Posted February 23, 2009 <?php function ui($username) { global $user; $query = mysql_query(" SELECT * FROM `users` WHERE `username` = '".mysql_real_escape_string($username)."' LIMIT 1 "); if (mysql_num_rows($query) == 1) { $user = mysql_fetch_array($query, MYSQL_ASSOC); return true; } else { return false; } } // #### // Example if (ui('test')) { echo htmlspecialchars($user['username']); } else { echo "Sorry, info for this user not found."; } ?> Not exactly what you want but you get the idea. Quote Link to comment https://forums.phpfreaks.com/topic/146574-issues-carrying-over-variable/#findComment-769525 Share on other sites More sharing options...
Philip Posted February 23, 2009 Share Posted February 23, 2009 <?php function ui($username) { global $user; $query = mysql_query(" SELECT * FROM `users` WHERE `username` = '".mysql_real_escape_string($username)."' LIMIT 1 "); if (mysql_num_rows($query) == 1) { $user = mysql_fetch_array($query, MYSQL_ASSOC); return true; } else { return false; } } // #### // Example if (ui('test')) { echo htmlspecialchars($user['username']); } else { echo "Sorry, info for this user not found."; } ?> Not exactly what you want but you get the idea. Pretty much right, but for myself I would do a return with an array of the variables instead of using global. <?php function ui($user) { // added limit 1, since you only need one row, yes? $query = "select * from users where username = '".$user."' LIMIT 1"; $result = mysql_query($query) or die ("Could not match data because ".mysql_error()); // if there weren't any rows, return false, otherwise return the array if(mysql_num_rows($result) < 1) return false; else return mysql_fetch_array($result); } // call functions $userInfo = ui('some_username'); // if it was returned as an array, we have data! if(is_array($userInfo)) print_r($userInfo); else echo 'no data for this user!'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/146574-issues-carrying-over-variable/#findComment-769529 Share on other sites More sharing options...
akirablaid Posted February 23, 2009 Author Share Posted February 23, 2009 You were all a great help, and after some experimentation with all suggestions I got this working perfectly. Thank you all! Quote Link to comment https://forums.phpfreaks.com/topic/146574-issues-carrying-over-variable/#findComment-769549 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.