Jump to content


Photo

easy newb question


  • Please log in to reply
6 replies to this topic

#1 Sorthy359

Sorthy359
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 June 2006 - 12:20 PM

hi :)
i have a really newbie question.

I dont understand why this code below does not work.
Well, everything appears fine as if $userip and the mysql stuff didnt even exist. I'm sure i uploaded the
file correctly, but im not getting any errors.
My problem is though, im also not seeing the ip field being updated in mysql.
I would also like to mention that this mysql connection is being brought up from a different file so i dont need to add anything about connecting there.
Is my syntax wrong for $sql = blablabla?
if not, what should i do to be able to add the ip address to that user's mysql table?
if($session->logged_in){
   echo "&nbsp;&nbsp;<a href=\"userinfo.php?user=$session->username\">My Account</a><br>";
   echo "&nbsp;&nbsp;<a href=\"process.php\">Logout</a><br>";
   $userip = $_SERVER['REMOTE_ADDR'];
   $sql = 'UPDATE "users" SET "ip" = "$userip" WHERE "username" = "$session->username"';
   mysql_query( $sql );
}


#2 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 14 June 2006 - 12:27 PM

$sql = 'UPDATE "users" SET "ip" = "$userip" WHERE "username" = "$session->username"';
   mysql_query( $sql );

Not overly familiar with mysql, but in mssql the below would be correct. I assume that mysql is near identical.

$sql = "UPDATE users SET ip = '$userip' WHERE username = '$session->username';";



#3 Sorthy359

Sorthy359
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 June 2006 - 12:29 PM

woot. thats it. thanks :)

#4 Sorthy359

Sorthy359
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 June 2006 - 12:51 PM

how would i select that from the db and echo it?

im using this on the menu as an example but i plan to use this for userinfo.php for admins ;)

this below outputs
Admin
  Admin Center   
(test) Your IP: Resource id #15

here is the code.
   if($session->isAdmin()){
      $q2 = "SELECT ip FROM users WHERE username = '$session->username'";
      $result2 = mysql_query($q2);
      echo "<br><b>&nbsp;&nbsp;<u>Admin</u></b><br>&nbsp;&nbsp;<a href=\"admin/admin.php\">Admin Center</a> &nbsp;&nbsp;";
      echo "<br>(test) Your IP: $result2";
   }

..what did i do wrong with that SELECT line? :(

#5 umbrella_thing

umbrella_thing
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 14 June 2006 - 01:05 PM

You need to put the result resource into another function to get field in a given row...

if($session->isAdmin()){
      $q2 = "SELECT ip FROM users WHERE username = '$session->username'";
      $result2 = mysql_query($q2);
     $ip_address = mysql_result($result2, 0, "ip");
      echo "<br><b>  <u>Admin</u></b><br>  <a href=\"admin/admin.php\">Admin Center</a>   ";
      echo "<br>(test) Your IP: $result2";
   }


$ip_address = mysql_result($result2, 0, "ip");

mysql_result(RESULT RESOURCE, ROW, FIELD);

that should do the trick

#6 Chips

Chips
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 14 June 2006 - 01:08 PM

[!--quoteo(post=383743:date=Jun 14 2006, 07:51 AM:name=Sorthy359)--][div class=\'quotetop\']QUOTE(Sorthy359 @ Jun 14 2006, 07:51 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
how would i select that from the db and echo it?

im using this on the menu as an example but i plan to use this for userinfo.php for admins ;)

this below outputs
Admin
  Admin Center   
(test) Your IP: Resource id #15

here is the code.
   if($session->isAdmin()){
      $q2 = "SELECT ip FROM users WHERE username = '$session->username'";
      $result2 = mysql_query($q2);
      echo "<br><b>&nbsp;&nbsp;<u>Admin</u></b><br>&nbsp;&nbsp;<a href=\"admin/admin.php\">Admin Center</a> &nbsp;&nbsp;";
      echo "<br>(test) Your IP: $result2";
   }

..what did i do wrong with that SELECT line? :(
[/quote]

All you've done is carry out a query, not actually returned the value itself. For that you'd need an extra line or two - like this:
$q2 = mysql_query("SELECT ip FROM users WHERE username = '$session->username';");
      $result2 = mysql_result($s2, 0, "ip");
echo $result2;
The mysql_result(your query, num, field name)
num is representative of the number of returned results. There should be only 1, but if you returned (for strange example) 200, you could do a loop with this set to a variable as:

for($i=0; $i < msql_num_rows($q2);$i++) {
echo mysql_results($s2, $i, "ip") . "<br />";
}

That should print out a list of IP address that match (for example if you selected everything from the database instead, without any parameters). You have parameters to your query to return just one result, hence the 0 being used.

Hope that works, if I am wrong I am sure someone with more competance will reply shortly :)

#7 Sorthy359

Sorthy359
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 14 June 2006 - 01:19 PM

ahh ok. =D i did a big google search and looked everywhere and saw a couple of answers but didnt quite catch what they meant, or understand the examples.. because most of the time it was someone helping someone else out with a big script.
Anyways..
thanks lotss




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users