Sorthy359 Posted June 14, 2006 Share Posted June 14, 2006 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 thefile 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 " <a href=\"userinfo.php?user=$session->username\">My Account</a><br>"; echo " <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] Quote Link to comment https://forums.phpfreaks.com/topic/11970-easy-newb-question/ Share on other sites More sharing options...
Chips Posted June 14, 2006 Share Posted June 14, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/11970-easy-newb-question/#findComment-45474 Share on other sites More sharing options...
Sorthy359 Posted June 14, 2006 Author Share Posted June 14, 2006 woot. thats it. thanks :) Quote Link to comment https://forums.phpfreaks.com/topic/11970-easy-newb-question/#findComment-45475 Share on other sites More sharing options...
Sorthy359 Posted June 14, 2006 Author Share Posted June 14, 2006 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> <u>Admin</u></b><br> <a href=\"admin/admin.php\">Admin Center</a> "; echo "<br>(test) Your IP: $result2"; }[/code]..what did i do wrong with that SELECT line? :( Quote Link to comment https://forums.phpfreaks.com/topic/11970-easy-newb-question/#findComment-45481 Share on other sites More sharing options...
umbrella_thing Posted June 14, 2006 Share Posted June 14, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/11970-easy-newb-question/#findComment-45484 Share on other sites More sharing options...
Chips Posted June 14, 2006 Share Posted June 14, 2006 [!--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> <u>Admin</u></b><br> <a href=\"admin/admin.php\">Admin Center</a> "; 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 :) Quote Link to comment https://forums.phpfreaks.com/topic/11970-easy-newb-question/#findComment-45485 Share on other sites More sharing options...
Sorthy359 Posted June 14, 2006 Author Share Posted June 14, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/11970-easy-newb-question/#findComment-45490 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.