Vinyl Posted August 4, 2007 Share Posted August 4, 2007 Hello everyone. I'm new to the forums and to php. I am learning at a slow pace and I was doing well, but I've run into something that I've not been able to figure out. Here's a copy of my code: <?php $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; if ($ipsearch = mysql_query("SELECT ip FROM table WHERE (ip = '$ip')")); { if ($ipsearch = $ip); { $name = mysql_query("SELECT name FROM table WHERE (ip = '$ip')")); } } else ($hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); { if (substr("$hostname", 0, 2) == 'P1') { $name = "property1"; } else if (substr("$hostname", 0, 2) == 'P2') { $name = "property2"; } } mysql_query ("INSERT INTO table VALUES ('$ip','$namel',1,now()) ON DUPLICATE KEY UPDATE hits=hits+1") or die(mysql_error()); mysql_close($con); ?> Basically, I want the page to check for the user's IP. Then it will check my DB to see if that IP exists, and if it does, it will set $name accordingly. If it doesn't exist, it will then get the user's Computer Name and determine the correct name. After all that it will either place the info in the DB for the first time, or update it and then move on to the next page in the site. According to what I've read and done research on, it seems to be right, but I dont' know enough about it to test it, and I've never been able to get my errors to dump correctly, so I'm simply stuck. Any help would be great, and I'm open to all suggestions! Also - I'm using PHP 5 and Apache 2.2 on Win XP if that helps. When I execute my page with that code, it does not do anything. Simply says "done." If I take that code out, it works fine, but doesn't do what I want! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/ Share on other sites More sharing options...
AndyB Posted August 4, 2007 Share Posted August 4, 2007 http://www.tizag.com/mysqlTutorial/mysqlquery.php You query the database, but never retrieve the results and abstract field values. The link above explains what to after after the query. Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/#findComment-315650 Share on other sites More sharing options...
Vinyl Posted August 5, 2007 Author Share Posted August 5, 2007 Ok, thanks for the info. I tried to rework it as follows, but I'm still missing something I think: <?php $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; if ($ipsearch = mysql_query("SELECT ip FROM table WHERE (ip = '$ip')") or die(mysqlerror())); { if (!$ipsearch); { echo "Could not run query:" . mysql_error(); exit; } $ipresult = mysql_fetch_row($ipsearch); if ($ipresult = $ip); { $hotel = mysql_query("SELECT name FROM table WHERE (ip = '$ip')") or die(mysql_error()); } } else ($hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); { if (substr("$hostname", 0, 2) == 'P1') { $name = "property1"; } else if (substr("$hostname", 0, 2) == 'P2') { $name = "property2"; } } mysql_query ("INSERT INTO table VALUES ('$ip','$name',1,now()) ON DUPLICATE KEY UPDATE hits=hits+1") or die(mysql_error()); mysql_close($con); ?> Like I said, I'm still trying to get the hang of how mysql and php work together, so any help would be appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/#findComment-316088 Share on other sites More sharing options...
BlueSkyIS Posted August 5, 2007 Share Posted August 5, 2007 It looks like you're expecting $ipresult to contain a single value when it will actually contain an entire row. You might try this instead: list($ipresult) = mysql_fetch_row($ipsearch); ...and there is no need to check $ipsearch a second time since you already tested with if ($ipsearch... you probably don't need this: if (!$ipsearch); { echo "Could not run query:" . mysql_error(); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/#findComment-316100 Share on other sites More sharing options...
Vinyl Posted August 5, 2007 Author Share Posted August 5, 2007 I see what you're saying. I am trying to get a single value assigned to $ipresult. Basically - I want it to check the DB for their IP, and if it's there, assign a name to $name. If it's not, continue with the process - the else ($hostname.. part. I fear I'm making it too complicated, but what's the best way to achieve a single result if I'm not doing it right? Maybe something like: if ($search = mysql_query("SELECT name FROM table WHERE (ip = '$ip')") or die(mysql_error()); { $search = $name; } else (.... ?? Oh, and thanks for pointing out that redundant error check. Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/#findComment-316231 Share on other sites More sharing options...
teng84 Posted August 5, 2007 Share Posted August 5, 2007 $search = mysql_query("SELECT name FROM table WHERE (ip = '$ip')");/dont put die if ($search) { //do something here } note dont put a die if you will filter the query that way it will alway be true mysql_query returns boolean datatypes but when you ad die when it becomes false it will be overwriten by the die value so this if ($search) means if $search is not empty and for sure its not because it has the die value Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/#findComment-316272 Share on other sites More sharing options...
Vinyl Posted August 6, 2007 Author Share Posted August 6, 2007 Holy crap - awesome. I finally got it: <?php $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; $ipsearch = mysql_query("SELECT ip FROM table WHERE (ip = '$ip')"); $result = mysql_fetch_row($ipsearch); $ipfinal = $result[0]; if ($ipfinal = $ip) { $namesearch = mysql_query("SELECT name FROM table WHERE(ip = '$ip')"); $nameresult = mysql_fetch_row($namesearch); $name = $nameresult[0]; } else $hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); if (substr("$hostname", 0, 2) == 'P1') { $name = "property1"; } else if (substr("$hostname", 0, 2) == 'P2') { $name = "property2"; } mysql_query ("INSERT INTO table VALUES ('$ip','$name',1,now()) ON DUPLICATE KEY UPDATE hits=hits+1") or die(mysql_error()); mysql_close($con); ?> Once I figured out the syntax to get it to set the final variables to one result only, I am gold. Works perfectly! Thanks for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/#findComment-316356 Share on other sites More sharing options...
teng84 Posted August 6, 2007 Share Posted August 6, 2007 then mark this as solved Quote Link to comment https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/#findComment-316360 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.