Jump to content


Photo

help with coding


  • Please log in to reply
4 replies to this topic

#1 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 16 July 2006 - 01:02 AM

ok im having a weird error with a code that somone gave me
and wanted to see if somone could help

heres the code:

<?php
   ob_start(); 
   $conn = mysql_connect("localhost","user","pass");
   mysql_select_db(database) or die(mysql_error());
   $logged = MYSQL_QUERY("SELECT * from users WHERE id='$_COOKIE[id]' AND password = '$_COOKIE[pass]'");
   $logged = mysql_fetch_array($logged);
   $fffquery = mysql_query("SELECT * FROM banned ");
   $bannedips = mysql_fetch_array($fffquery);
   if(in_array($_SERVER['REMOTE_ADDR'], $bannedips)) {  // this is line 9
	print("You have been banned from this website and are unable to view it.");
	exit();
   }
?> 

and heres the error:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/dreamsh/public_html/config.php on line 9


#2 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 16 July 2006 - 01:08 AM

um heres a question do u think it wont work if there isnt ne ips in there ?

#3 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 16 July 2006 - 01:08 AM

yes, that's right, if there isn't any ip in there, it'll give that error.

to fix the error, at least declare $bannedips an array:

$bannedips = array();
Life's too short for arguing.

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 16 July 2006 - 01:12 AM

it's because in_array() expects an actual array for its second parameter.  what the error means is that your $bannedips isn't an actual array.  there are a few possible reasons for it not being an array:

1.  your query is failing.  add an "or die(mysql_error())" clause to the end of your mysql_query() statement on line 7.  i have a feeling the extra space at the end of the query will throw an error.
2.  the query isn't returning any rows, in which case $bannedips will be empty.

regardless, i don't think this script will do what you want.  you have to go through each row in the `banned` table and assign the IP from that row to an array, like so:

<?php
$bannedips = array();
while ($row = mysql_fetch_array($fffquery))
{
  $bannedips[] = $row['ip_column_name'];
}
?>

this way, $bannedips is an array whether it returns any rows or not and it should avoid giving you an error for using in_array().  these lines can replace the line "$bannedips = mysql_fetch_array($fffquery);".  replace "ip_column_name" with whatever the name of the field you're storing the IP in is.

#5 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 16 July 2006 - 01:12 AM

ok thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users