473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to make it shorter and better?

Hello,,,

I don't know much about PHP+SQL but I have to find the better way to make my
query work and to be shorter and better.
All help will be appreciated.

//Check if IP exists in banIP
$res = mysql_query("SELECT ip FROM banIP WHERE ip =
'".$_SERVER['REMOTE_ADDR']."'");
$res = mysql_num_rows($res);
if($res != 0)
die;
mysql_free_result($res);

//Fetch Time
$timestamp = time();
$timeout = $timestamp - 900;
//Delete Users
$del = mysql_query("DELETE FROM caspionet WHERE timestamp<$timeout");

//Check if IP exists in caspionet
$res = mysql_query("SELECT ip FROM caspionet WHERE ip =
'".$_SERVER['REMOTE_ADDR']."'");
$res = mysql_num_rows($res);
if($res == 0){
//Insert User
$ins = mysql_query("INSERT INTO caspionet (timestamp, ip)
VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."')");
}
mysql_free_result($res);

//Fetch Users Online
$res = mysql_query("SELECT DISTINCT ip FROM caspionet");
while ($row = mysql_fetch_array($res, MYSQL_BOTH)){
print("IP:$row[0]");
}
mysql_free_result($res);

Thank you
A.K.
Apr 29 '06 #1
6 1290
In article <e2**********@news.tiscali.fr>, "AlGorr" <a@a.com> wrote:
<snip>


It's pretty tight as it is.
# Setup
$ip = $_SERVER["REMOTE_ADDR"];
$timestamp = time()-900;

# Should we die?
$c = mysqlquery("select count(id) from banIP where ip = '$ip'");
list ($count) = mysql_fetch_assoc($c);
if ($count) die;

# Remove old stuff
mysql_query("delete from caspionet where timestamp < $timestamp");

# In Caspionet?
$c = mysqlquery("select count(id) from caspionet where ip = '$ip'");
list ($count) = mysql_fetch_assoc($c);
if (!$count){
mysql_query("insert into caspionet...");
}

# Who's online?
$ips = mysql_query("select distinct ip from caspionet");
while ($r = mysql_fetch_assoc($ips)){
print "IP: $r[ip]\n";
}


--
Sandman[.net]
Apr 29 '06 #2
Sandman thanks a lot !!!!!!!!!!!!!!!!!!!
That's much better! I will use your exemple.

Only look:
we have to ask 4 times the server for these 4 queries. It's a SQL question.
Is it possible to put for ex. 2 queries in one big and make work the server
more faster?

Regards
A.K.
Apr 29 '06 #3
AlGorr wrote:
Hello,,,

I don't know much about PHP+SQL but I have to find the better way to make my
query work and to be shorter and better.
All help will be appreciated.

//Check if IP exists in banIP
$res = mysql_query("SELECT ip FROM banIP WHERE ip =
'".$_SERVER['REMOTE_ADDR']."'");
$res = mysql_num_rows($res);
if($res != 0)
die;
mysql_free_result($res);

//Fetch Time
$timestamp = time();
$timeout = $timestamp - 900;
//Delete Users
$del = mysql_query("DELETE FROM caspionet WHERE timestamp<$timeout");

//Check if IP exists in caspionet
$res = mysql_query("SELECT ip FROM caspionet WHERE ip =
'".$_SERVER['REMOTE_ADDR']."'");
$res = mysql_num_rows($res);
if($res == 0){
//Insert User
$ins = mysql_query("INSERT INTO caspionet (timestamp, ip)
VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."')");
}
mysql_free_result($res);

//Fetch Users Online
$res = mysql_query("SELECT DISTINCT ip FROM caspionet");
while ($row = mysql_fetch_array($res, MYSQL_BOTH)){
print("IP:$row[0]");
}
mysql_free_result($res);

Thank you
A.K.


Please cross-post - don't multipost!

Answered in comp.databases.mysql.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 29 '06 #4
In article <e2**********@news.tiscali.fr>, "AlGorr" <a@a.com> wrote:
Sandman thanks a lot !!!!!!!!!!!!!!!!!!!
That's much better! I will use your exemple.

Only look:
we have to ask 4 times the server for these 4 queries. It's a SQL question.
Is it possible to put for ex. 2 queries in one big and make work the server
more faster?


Nah, not more efficient. It's still four queries with four different
rules. They all need to be executed separately.

--
Sandman[.net]
Apr 29 '06 #5
Sandman wrote:
In article <e2**********@news.tiscali.fr>, "AlGorr" <a@a.com> wrote:
Sandman thanks a lot !!!!!!!!!!!!!!!!!!!
That's much better! I will use your exemple.

Only look:
we have to ask 4 times the server for these 4 queries. It's a SQL
question. Is it possible to put for ex. 2 queries in one big and make
work the server more faster?


Nah, not more efficient. It's still four queries with four different
rules. They all need to be executed separately.


Actually you could do strange things with cartesian products but it may be
messy depending on how the database is set up. Presumably if AlGorr needs
to look at it it's because there is a performance issue. First place to
start with performance issues in database queries is.....the database. Not
the application language.

C.
Apr 30 '06 #6
In article <aJ*************@newsfe2-gui.ntli.net>,
Colin McKinnon
<co**********************@ntlworld.deletemeunlessU RaBot.com> wrote:
Sandman wrote:
In article <e2**********@news.tiscali.fr>, "AlGorr" <a@a.com> wrote:
Sandman thanks a lot !!!!!!!!!!!!!!!!!!!
That's much better! I will use your exemple.

Only look:
we have to ask 4 times the server for these 4 queries. It's a SQL
question. Is it possible to put for ex. 2 queries in one big and make
work the server more faster?


Nah, not more efficient. It's still four queries with four different
rules. They all need to be executed separately.


Actually you could do strange things with cartesian products but it may be
messy depending on how the database is set up. Presumably if AlGorr needs
to look at it it's because there is a performance issue. First place to
start with performance issues in database queries is.....the database. Not
the application language.


Indeed - I didn't interprete "shorter and better" as being that,
though. BUt now that you say it...

Indexes is a good place to start for single-value lookups

--
Sandman[.net]
May 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
9
by: John Salerno | last post by:
Just wondering if this will ever happen, maybe in 3.0 when print becomes a function too? It would be a nice option to have it available without importing it every time, but maybe making it a...
7
by: aviad | last post by:
I am writing a Form application I need it to fit both resolution of 1600*1200 and 800*600 (and any other resolution that might jump in) the application is meant for regular PCs another question...
3
by: salad | last post by:
I have an A97 application that is NOT split on a network. It is used by 15+ folks continually. It is quick and fast. I split it several years ago and had to merge it together again after the...
0
by: YellowFin Announcements | last post by:
Security solutions provider EXTOL MSC Berhad has developed a neural network predictive analysis engine and is now working with Australian business intelligence company Yellowfin to develop a front...
3
by: skanemupp | last post by:
is there anyway to make this shorter? i hate having these big blocks of similar-looking code, very unaesthetic. maybe doesnt matter good-code-wise? anyway can i make some function that makes this...
4
by: raylopez99 | last post by:
Please consider the below and how to make name references shorter-- there has to be a way. RL using System; namespace MyNamesSpace1 { class ManagerClass
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.