Jump to content

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)

 

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.