View Full Version : Check a whole database for something?
lolwut
17-11-2007, 12:45 PM
When my users fill in this form on our site their IP gets put in the database along with the other data....
How could I make it check to see if their IP is already in the database?
EDIT: Atm I've got:
$check = mysql_query("SELECT `ip` FROM `entries` WHERE `ip` = '$ip' ;");
if(isset($check)){ die('You cannot enter more than once!'); }
Any help appreciated.
Luno1599
17-11-2007, 12:49 PM
ohhhhhh i know this :S but forgotten... Give me a min
Edited by opensourcehost (Forum Super Moderator): Please do not post pointlessly.
Jamie.
17-11-2007, 12:58 PM
When my users fill in this form on our site their IP gets put in the database along with the other data....
How could I make it check to see if their IP is already in the database?
EDIT: Atm I've got:
$check = mysql_query("SELECT `ip` FROM `entries` WHERE `ip` = '$ip' ;");
if(isset($check)){ die('You cannot enter more than once!'); }
Any help appreciated.
$fetch7 = mysql_query("SELECT `ip` FROM `entries` WHERE `ip` = '$ip'")
or die(mysql_error());
$fetch8 = mysql_num_rows($fetch7);
if ($fetch8 == 0) {
//the ip doesnt exist }
else {
//the ip exists
}
QuickScriptz
17-11-2007, 03:05 PM
Try a php look:
PSEUDO CODE:
<?php
while($dbs = get_mysql_table_list('from_db');){
//Do your check here
if(entry = found){
$found = 'yup';
}
}
if($found == "yup"){
//It exists!
}else{
/Sorry, nothing...
}
?>
Note that is pseudo code - therefore it wont actually parse or work, it is just meant to explain the logic behing what I'm suggesting.
if( mysql_num_rows($sql) > 0 )
{
// Error
} else
{
// Stuff
}
Dentafrice,
17-11-2007, 04:15 PM
<?php
include "config.php"; // includes config file
$check_i = mysql_query("SELECT ip FROM entries WHERE ip='$ip'");
$check_n = mysql_num_rows($check_i);
if ($check_n == "" || $check_n == "0")
{
echo "You cannot enter more then once!";
exit;
}
?>
<?php
include "config.php"; // includes config file
$check_i = mysql_query("SELECT ip FROM entries WHERE ip='$ip'");
$check_n = mysql_num_rows($check_i);
if ($check_n == "" || $check_n == "0")
{
echo "You cannot enter more then once!";
exit;
}
?>
mysql_num_rows() returns an integar, and only an integar. There's no need to check for empty strings, or to treat the 0 like a string, by enclosing it with quotes.
Dentafrice,
17-11-2007, 08:34 PM
It doesn't matter, thats the way I do it.
It doesn't matter, thats the way I do it.
Well, it's a waste of server resources, time, and most importantly, waste the energy your fingers have to go to typing it in :P
if(mysql_num_rows($query) > 0) {
// Rows were returned
}
else {
// No rows returned (0)
}
That's all you need to do to check for rows returned :)
Jamesy
17-11-2007, 08:47 PM
haha that's like the script you told me last night.
You are good!
Luno1599
17-11-2007, 08:51 PM
From what i have seen the script works so what are you arguing about :S
From what i have seen the script works so what are you arguing about :S
Well, it's slightly off-topic, but if we have a disagreement with a coder's methods, we normally let them know after they've posted it.
Dentafrice,
17-11-2007, 08:56 PM
Well, it's a waste of server resources, time, and most importantly, waste the energy your fingers have to go to typing it in :P
if(mysql_num_rows($query) > 0) {
// Rows were returned
}
else {
// No rows returned (0)
}
That's all you need to do to check for rows returned :)
I have worked with PHP probably years more then you have.
I think I know how to code.
Plus why add the num_rows into the if statement, thats a waste of typing plus server resources if you are expecting to use the num rows again :)
So please don't tell me how to code, when I know how to :)
Luno1599
17-11-2007, 08:56 PM
Oh yeah XD i get confused easy :P
I have worked with PHP probably years more then you have.
I think I know how to code.
Plus why add the num_rows into the if statement, thats a waste of typing plus server resources if you are expecting to use the num rows again :)
So please don't tell me how to code, when I know how to :)
I'm not saying you don't know how to code, I'm just letting you know that it's a waste of time checking for empty strings, when mysql_num_rows will never return an empty string.
The code was used as an example. Of course, if you were expecting to use the data again, you would store it in a variable. However, even if I was modelling the code off Waterfall's question, from the looks of things, he wouldn't need to use it again (why would he need to check for multiple IP entries in a script twice)? I suggest you stop the "I've been coding for years more than you" ploy. The length of time someone has been practising at something is a bad portrayal of skill, as seen here.
Dentafrice,
17-11-2007, 09:04 PM
No actually it isn't.
If you have no DB connection, and you do mysql_num_rows and error reporting is off, you would receive an empty string ;)
Also if you do num_rows without an actual query, and without any error reporting it returns an empty string.
Luno1599
17-11-2007, 09:06 PM
That is true.. because it dose not exist
Dentafrice,
17-11-2007, 09:10 PM
I think thats the reason I started doing it that way to begin with.
No actually it isn't.
If you have no DB connection, and you do mysql_num_rows and error reporting is off, you would receive an empty string ;)
Also if you do num_rows without an actual query, and without any error reporting it returns an empty string.
... That's one of the most ridiculous arguments I've heard. If there wasn't a database connection in the first place, you'd have a lot more to worry about than what mysql_num_rows was returning. In addition, the entire script would be rendered useless, because it would be unable to perform queries needed to function properly.
I would bet a large sum of money that software developers like Jelsoft, Invision and the like don't test for empty strings, when validating mysql_num_rows results, for the reason that it is so petty. If they don't, then there surely is no point.
Dentafrice,
17-11-2007, 09:31 PM
Look, it doesn't matter.. I use it, you don't.. it doesn't change a thing. I will still use it, you don't have to. ;)
Luno1599
17-11-2007, 09:33 PM
Look, it doesn't matter.. I use it, you don't.. it doesn't change a thing. I will still use it, you don't have to. ;)
Well thats that then all sorted XD
Jamesy
17-11-2007, 10:06 PM
Wow. Coder cat fight...
Luno1599
18-11-2007, 11:34 AM
Wow. Coder cat fight...
haha as im new to this forum i think it was pritty funny
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.