Forum Moderators: coopster

Message Too Old, No Replies

Help with Mysql query used in PHP

         

rscrsc

2:58 am on Mar 27, 2006 (gmt 0)

10+ Year Member



Hello everyone, I have the following query. I am stuck on the following problem. The script below works great currently. I am trying to edit the script to that a user does not have to enter all the information and the script will still run. For example an user is asked for an IP address, start date and end date to search. The problem is the query should still print out results if the user just enters the IP address and not the dates, or if the user enters just the dates and no IP addresses.

$query= "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%') and (inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."')";

THANKS FOR THE HELP IDEAS!

Habtom

4:32 am on Mar 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is not the best solution, but still works :)
if (ONLY IP Address is set) {
$query = "SELECT. . . "
}
else if {
Query when ONLY Dates are set
}
else {
Query when both set
}

Habtom

coopster

12:51 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or you could just start the query statement and append the WHERE clause using the same type of logic provided by Habtom.
$query = "SELECT ... FROM table "; 
$wherestatement = ''; // initialize
if (ONLY IP Address is set) {
$wherestatement = 'WHERE ip ...';
} else if (ONLY Dates are set) {
$wherestatement = 'WHERE theDates ...';
} else {
//when both set
$wherestatement = 'WHERE ip ... AND theDates ...";
}
$query .= $wherestatement;

rscrsc

9:50 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



Here is what I changed the code to. I am unable to get any results. Can anyone point me in the right direction as to where/what is wrong with this code? Am I supposed to print the results after every if statement?

if {
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."'";
}
else if {
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%')";
}
else {
"select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%') and (inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."')";
}
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';
}
?>

THANKS AGAIN FOR THE HELP!

rscrsc

10:11 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



In addition to the sample code above I have also tried the following code with no LUCK!

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
//Query that is being run
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event";
$wherestatement = ''; //initialize
if (ONLY IP Address is set) {
$wherestatement = "where inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."'";
} else if (ONLY Dates are set) {
$wherestatement = "where (timestamp between '" .$Sdate."%' and '" .$Edate."%')";
} else {
//when both set
$wherestatement = "where (timestamp between '" .$Sdate."%' and '" .$Edate."%') and (inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."')";
}
$query .= $wherestatement;
//this will try to spit out the previous sql query
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';
}
?>

thanks!

Habtom

4:35 am on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have got to replace the "ONLY IP Address is set" with the appropriate code, so do you need to replace "ONLY Dates are set" too. I just wrote those to show you how the structure will look like.

The syntax will be something like:
if ($_REQUEST['date']!= "") { }

OR

if ($_isset['date']!= "") { }

Habtom