Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/63344-solved-mysql-query-question/
Share on other sites

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!

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;

}

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.

$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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.