View Full Version : what am i doing wrong
jmyeom
08-03-2009, 09:30 PM
here is my code:
<?
// ######################### Sql login ######################### \\
mysql_connect("INFO", "INFO", "INFO") or die(mysql_error());
mysql_select_db("INFO") or die(mysql_error());
// ###################### Global variables ##################### \\
$c1 = 0;
$c2 = 0;
$array1 = array();
$array2 = array();
// ######################### Sql query ######################### \\
$query1 = mysql_query("SELECT * FROM `user`") or die (mysql_error ());
while($query = mysql_fetch_array($query1) or die (mysql_error ())){
// Checks to see if ip is already in list
if (!array_key_exists($query["ipaddress"], $array1)) {
// reads the IP and saves to array
$array1[$c1] = $query["ipaddress"];
// Changes the number in which the ip is saved to the array
$c1 = $c1 + 1;
} ELSEIF (!array_key_exists($query["ipaddress"], $array2)) { // Checks to see if Username is already in list
// reads the Username and saves to array
$array2[$c2] = $query["ipaddress"];
// Changes the number in which the Username is saved to the array
$c2 = $c2 + 1;
}
}
echo $array2;
?>
its on loops to see if the ips are in the database already, e.g checking for multi accounts
if the ips is not in the array, it adds it in, but if it is already in the array, it adds it to a secand array, it then echos the secand array when done, showing everyones ip that is in there more then once,
the secand check is to see if the ip is allready in the secand array, so it will only show ech ip once,
and for some reason, im getting nothing ?
any help/hints?
i am sure the databse has multi ips, cos its the reason i made this ..?
thanks in advance:
Jmyeom
MattF
08-03-2009, 09:39 PM
For starters, ditch the die statement on this line:
while($query = mysql_fetch_array($query1) or die (mysql_error ())){
You only need that when you're doing the query, not when you're pulling the rows from the DB. Plus, (I've only given the code a brief once over by the way, so this may not be relevant), why not just write the query to select the info from the DB only when either that name or I.P already have an entry rather than selecting all rows and looping through?
jmyeom
08-03-2009, 09:44 PM
... i dident know there was a query for this...
can you show me the query please?
MattF
08-03-2009, 09:48 PM
Anything within brackets, [], in the following code needs altering for your specific syntax, (including removing the brackets).
$query1 = mysql_query("SELECT * FROM `user` WHERE ([ip_address]='[ip_address]' OR [username]='[username]')") or die (mysql_error ());
The value preceding the = sign is the column name.
jmyeom
08-03-2009, 09:51 PM
but this would mean you would have to know the ip first?... but if we dont know the ip, my script searchs them...
its on loops to see if the ips are in the database already, e.g checking for multi accounts
To do this, the script must know the IP address so, I reckon Matt's suggestion is what you are looking for.
bazz
MattF
08-03-2009, 09:59 PM
Aah, my apologies. I completely misinterpreted your post. :) I'm obviously having one of those days again. :D
jmyeom
08-03-2009, 10:01 PM
matt might be currect in his thinking, but i dont know the ip address,
it reads the ip from the databse
if the ip is not in the array, it adds it to the array
but if it is allready in the array, then it knows it a multi account so adds it to array 2
but if its allready in array 2, that means we allready know its a multi account, and no need to add the ip again
i can get a list of ips from the database, but it seems im having trouble echoing that array -.-
MattF
08-03-2009, 10:03 PM
To print an array, use print_r() or a foreach loop to loop through the array and print the values one by one.
jmyeom
08-03-2009, 10:06 PM
kk, well i removed it so it dont add to array, instead if its allready in the array, just print it using echo, and it prints... nothing,
i checked the sql, there is multi accoutns..
am i useing the array_key_exists($query["ipaddress"], $array1
in the right way?
Aah, my apologies. I completely misinterpreted your post. :) I'm obviously having one of those days again. :D
I must be too coz I still read it as I did firstly. If you are running a loop to check db records against a variable, then your initial query can do that in one step.
select col1
from table
where ip_record = [ip_address]
However if you have set your IP address as a primary key, to prevent duplicates, you can be sure there aren't any. ;)
bazz
jmyeom
08-03-2009, 10:15 PM
primary key = account number
select col1
from table
where ip_record = [ip_address]
again, you need to know the ip in advance..
MattF
08-03-2009, 10:25 PM
I must be too coz I still read it as I did firstly. If you are running a loop to check db records against a variable, then your initial query can do that in one step.
Aye, that's how I interpreted it first off. :D What he's actually doing is pulling the I.P addresses, (and usernames?), from the DB and looping to check if they exist more than once in the DB.
MattF
08-03-2009, 10:32 PM
Try this instead:
while ($query = mysql_fetch_assoc($query1))
{
if (!in_array($query['ipaddress'], $array1))
{
$array1[] = $query['ipaddress'];
}
else
{
if (!in_array($query['ipaddress'], $array2))
{
$array2[] = $query['ipaddress'];
}
}
}
Add a similar if/else setup for the username within the while loop if you want to check the usernames also.
jmyeom
08-03-2009, 10:38 PM
yes:
here is my code now
$query1 = mysql_query("SELECT * FROM `user`") or die (mysql_error ());
while($query = mysql_fetch_array($query1)){
//Stores the IP
$IP = $query["ipaddress"];
// Checks to see if ip is already in list
if (array_key_exists($IP, $array1) == false) {
// reads the IP and saves to array
$array1[$c1] = $IP;
echo $IP . "section1</br>";
// Changes the number in which the ip is saved to the array
$c1 = $c1 + 1;
} elseif (array_key_exists($IP, $array1) == true) {
//IF (!array_key_exists($IP, $array2)) { // Checks to see if Username is already in list
// reads the Username and saves to array
//$array2[$c2] = $IP;
echo $IP . "section2</br>";
// Changes the number in which the Username is saved to the array
//$c2 = $c2 + 1;
//}
}
}
//print_r($array1);
so i made it so it shows what section it went to, and every ip goes to section 1, although a ip appears 2 twince in the section 1 list, so it looks like that function is thw wrong tool for the job ... any other ideas?
MattF
08-03-2009, 10:40 PM
The array key can never match the I.P address in your code. Try the code I posted above.
jmyeom
08-03-2009, 10:41 PM
ty MattF !
i replace that array_key_exists with in array and it works!!!!!
ty! <3
is there a thanks button somewherE?
That'll the the one that says "Thank user for this helpful post" on the right hand side :)
jmyeom
08-03-2009, 10:43 PM
also, where you put array[], does that automaticly add the ip to the end of the array :)?
jmyeom
08-03-2009, 10:45 PM
i feel like a moron, not only did i fail at a basic php script, but i also cant find the thanks button -.-
edit:
found it :), the button did not load -.-
MattF
08-03-2009, 10:47 PM
That'll the the one that says "Thank user for this helpful post" on the right hand side :)
:D :D
also, where you put array[], does that automaticly add the ip to the end of the array
That does exactly the same as your code without you having to generate the key. The key is automatically incremented each time you add an entry, so you have no need to do it manually. :) Just add the bits you need to that code I posted. I've removed all of the unnecessary cruft for you.
i feel like a moron, ...
The world is full of them so you shouldn't have much trouble in finding one. :D
In case that doesn't translate to your dialect/language; it is a compliment!!
bazz
jmyeom
08-03-2009, 10:50 PM
well, thanks to all who helped, most likly you all felt like jumping on me and beat it into me, (and thanks for not doing so) :)
MattF
08-03-2009, 10:51 PM
i feel like a moron, not only did i fail at a basic php script, but i also cant find the thanks button -.-
Don't worry about it. :D In all honesty, arrays are one of the most awkward parts in PHP to get to grips with. They seem simple enough when you finally get used to them and realise how they work, but they are absolute buggers to learn initially.
hey, we we're all learners. They day we stop learning is the day we are obsolete. And, if you can't come to CF to learn, where on 'earth' can you go??
bazz
jmyeom
08-03-2009, 10:58 PM
im not sure, i dont think i would of ever solved it without you guys,
also, i dient know you could just do [] and it would add to the end of the array, me though you had to tell it the numebr to add to,
and i dident know "in_array" was a functions, it seems so easy!
that was what was wrong all along...
so my script now looks liek this:
<?
// ######################### Sql login ######################### \\
mysql_connect("****", "****", "****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());
// ###################### Global variables ##################### \\
$array = array();
// ######################### Sql query ######################### \\
$query1 = mysql_query("SELECT * FROM `user`") or die (mysql_error ());
while($query = mysql_fetch_array($query1)){
// SETS VARIABLES
$IP = $query["ipaddress"] . "</br>";
// CHECKS TO SEE IF IP IS ALREADY IN LIST
if (!in_array($IP, $array)) { // IF FALSE
// ADDS IP TO LIST
$array[] = $IP;
} ELSE { // IF TRUE
//ECHOS IP
echo $IP;
}
}
?>
it seems so simple now :(
MattF
08-03-2009, 11:09 PM
That looks much cleaner. :) I would suggest changing your query line to this, however.
$query1 = mysql_query("SELECT ipaddress FROM `user`") or die (mysql_error ());
You're best off only selecting what you actually need from a table rather than everything.
MattF
08-03-2009, 11:10 PM
hey, we we're all learners. They day we stop learning is the day we are obsolete. And, if you can't come to CF to learn, where on 'earth' can you go??
Ne'er a truer word said.
jmyeom
08-03-2009, 11:13 PM
thanks for the query tip :)
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.