Jump to content

what's wrong with this query...


nobodyk

Recommended Posts

$aid = 394;
$uip = $_SERVER["REMOTE_ADDR"];
$sql="SELECT * FROM  allow WHERE  uid='$aid' ";
$result=mysql_query($sql);
$numrow = mysql_num_rows($result);
$row = mysql_fetch_row($result);
for ( $counter = 0; $counter <= $numrow; $counter++) {
if ($row[$counter] == $uip) {
$allowed = true;}
}

The table looks like this (both primary keys):

uid ip

394 23.432.23.1

394 23.321.32.12

345 343.343.213.12

234 34.54.3.233

123 3.4.342.343

 

This code checks if the user is allowed to enter a particular topic. From what I can tell is that $row[$counter] is not working properly. Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/198075-whats-wrong-with-this-query/
Share on other sites

I don't understand what $row[$counter] is.

If you are only allowing access to the user trying to login, if there IP is the same as the one logged in there row in the database.

Then maybe try..

 

$aid = 394;
$uip = $_SERVER["REMOTE_ADDR"];
$sql = "SELECT * FROM  allow WHERE  uid='$aid' ";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
if ($row['ip'] == $uip) {
      $allowed = true;
}

 

definitions:

mysql_fetch_row() - Get a result row as an enumerated array

mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both

mysql_fetch_assoc() - Fetch a result row as an associative array

mysql_fetch_object() - Fetch a result row as an object

mysql_data_seek() - Move internal result pointer

mysql_fetch_lengths() - Get the length of each output in a result

mysql_result() - Get result data

 

You'd use mysql_fetch_array, or mysql_fetch_assoc for what you were trying to do.

Why return all the rows for the user? Just look to see if the combination exists:

 

$aid = 394;
$uip = $_SERVER["REMOTE_ADDR"];
$sql="SELECT COUNT(*) as Allowed FROM  allow WHERE  uid='$aid'  AND ip =  '$uip'";
$result=mysql_query($sql);
$row = mysql_fetch_row($result);
$allowed = ($row['Allowed'] == 1);

 

Fewer steps, so it saves time (lots and lots of nano-seconds)

 

Archived

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

×
×
  • 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.