nobodyk Posted April 9, 2010 Share Posted April 9, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/198075-whats-wrong-with-this-query/ Share on other sites More sharing options...
trq Posted April 9, 2010 Share Posted April 9, 2010 You do understand that mysql_fetch_row returns 1 row of a result each time it is called? Quote Link to comment https://forums.phpfreaks.com/topic/198075-whats-wrong-with-this-query/#findComment-1039425 Share on other sites More sharing options...
TeddyKiller Posted April 9, 2010 Share Posted April 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198075-whats-wrong-with-this-query/#findComment-1039456 Share on other sites More sharing options...
DavidAM Posted April 9, 2010 Share Posted April 9, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/198075-whats-wrong-with-this-query/#findComment-1039483 Share on other sites More sharing options...
TeddyKiller Posted April 9, 2010 Share Posted April 9, 2010 Fewer steps, so it saves time (lots and lots of nano-seconds) Your very picky with your nano-seconds job. Heh. Although with my way it'll be easier to add an else. Quote Link to comment https://forums.phpfreaks.com/topic/198075-whats-wrong-with-this-query/#findComment-1039495 Share on other sites More sharing options...
nobodyk Posted April 9, 2010 Author Share Posted April 9, 2010 Thank You guys, this really helped me. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/198075-whats-wrong-with-this-query/#findComment-1039607 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.