Jump to content


Photo

simple query help


  • Please log in to reply
5 replies to this topic

#1 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 02 April 2006 - 04:05 AM

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?
$sessu = $_SESSION['username'];

    $sql = "SELECT id from users WHERE username = '$sessu'";


#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 02 April 2006 - 04:53 AM

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.

#3 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 02 April 2006 - 05:10 AM

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:

foreach($myplayers AS $newp) {

    $sql = "INSERT INTO myplayers (user_id, player_id) VALUES ('$id', $newp)";
    mysql_query ($sql) or die(mysql_error());
}





#4 Javizy

Javizy
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 02 April 2006 - 05:36 PM

Is the user ID an integer? You have it wrapped in inverted commas, so that would cause a problem.

#5 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 02 April 2006 - 05:52 PM

No, that won't cause a problem.

esiason, show me how you are loading up that $myplayers array.

#6 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 02 April 2006 - 11:43 PM

Thanks guys...I ended up figuring it out. this works for me. Not sure if it is efficient, but it works.

$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());
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users