Jump to content

Archived

This topic is now archived and is closed to further replies.

Sorthy359

easy newb question

Recommended Posts

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?
[code]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 );
}[/code]

Share this post


Link to post
Share on other sites
[code]$sql = 'UPDATE "users" SET "ip" = "$userip" WHERE "username" = "$session->username"';
   mysql_query( $sql ); [/code]

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

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

Share this post


Link to post
Share on other sites
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
[code]
Admin
  Admin Center  
(test) Your IP: Resource id #15[/code]

here is the code.
[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";
   }
[/code]

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

Share this post


Link to post
Share on other sites
You need to put the result resource into another function to get field in a given row...

[code]
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";
   }

[/code]

[code]$ip_address = mysql_result($result2, 0, "ip");[/code]

mysql_result(RESULT RESOURCE, ROW, FIELD);

that should do the trick

Share this post


Link to post
Share on other sites
[!--quoteo(post=383743:date=Jun 14 2006, 07:51 AM:name=Sorthy359)--][div class=\'quotetop\']QUOTE(Sorthy359 @ Jun 14 2006, 07:51 AM) [snapback]383743[/snapback][/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
[code]
Admin
  Admin Center  
(test) Your IP: Resource id #15[/code]

here is the code.
[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";
   }
[/code]

..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:
[code]
$q2 = mysql_query("SELECT ip FROM users WHERE username = '$session->username';");
      $result2 = mysql_result($s2, 0, "ip");
echo $result2;
[/code]
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:
[code]

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

That [i]should[/i] 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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.