esiason14 Posted April 2, 2006 Share Posted April 2, 2006 Trying to find the user id from the users table based on the session login name. This is what I have, but its not working. Any pointers?[code]$sessu = $_SESSION['username']; $sql = "SELECT id from users WHERE username = '$sessu'";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6381-simple-query-help/ Share on other sites More sharing options...
wickning1 Posted April 2, 2006 Share Posted April 2, 2006 put in an "echo $sql;" so you can see the query you're sending to the database. It's possible that your session management isn't working yet. Quote Link to comment https://forums.phpfreaks.com/topic/6381-simple-query-help/#findComment-23085 Share on other sites More sharing options...
esiason14 Posted April 2, 2006 Author Share Posted April 2, 2006 Thanks for the reply..session management is definitely working...I did a print_r(_$SESSION) and it returns my username and passwd. Also did an echo"$sql" and it returned my username.What I'm trying to do is get the user id from the above query and use it in the following query, which stores user entered info. The following query is storiing $newp in my db, but not the user id from the first query. Maybe my problem is how I'm using the results from the first query here:[code]foreach($myplayers AS $newp) { $sql = "INSERT INTO myplayers (user_id, player_id) VALUES ('$id', $newp)"; mysql_query ($sql) or die(mysql_error());}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6381-simple-query-help/#findComment-23089 Share on other sites More sharing options...
Javizy Posted April 2, 2006 Share Posted April 2, 2006 Is the user ID an integer? You have it wrapped in inverted commas, so that would cause a problem. Quote Link to comment https://forums.phpfreaks.com/topic/6381-simple-query-help/#findComment-23183 Share on other sites More sharing options...
wickning1 Posted April 2, 2006 Share Posted April 2, 2006 No, that won't cause a problem.esiason, show me how you are loading up that $myplayers array. Quote Link to comment https://forums.phpfreaks.com/topic/6381-simple-query-help/#findComment-23186 Share on other sites More sharing options...
esiason14 Posted April 2, 2006 Author Share Posted April 2, 2006 Thanks guys...I ended up figuring it out. this works for me. Not sure if it is efficient, but it works.[code]$sessu = mysql_escape_string($_SESSION['username']);$result = mysql_query("SELECT id from users WHERE username = '$sessu'"); if (!$result) { die('Invalid query: ' . mysql_error());}while ($row = mysql_fetch_array($result)){ $id = $row['id'];}foreach($player_id AS $newp) { $sql = "INSERT INTO myplayers (user_id, player_id) VALUES ($id, $newp)"; mysql_query ($sql) or die(mysql_error());}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6381-simple-query-help/#findComment-23303 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.