PHP / MYSQL help

  • Hey - turns out IRC is out and something a little more modern has taken it's place... A little thing called Discord!

    Join our community @ https://discord.gg/JuaSzXBZrk for a pick-up game, or just to rekindle with fellow community members.

Nekro

Not Long Now....
Mar 29, 2002
1,194
0
UK
i guess this place gets viewed the most, can anyone with some php mysql experience help me with this.

$query = "SELECT * FROM airline where ffrom = '$ffrom' OR fto = '$fto' OR flight_no = '$flight_no' OR departure = '$departure' OR arrival = '$arrival'";

As you can see on my current query it uses OR, so the user on the webpage can jus type in one thing and it will show, but i want to make the searches more precise so they can enter 2 out of the 5 things or more if they like. If i use AND for this and the user leaves a field blank, then it will return no results as it sees the user enter " ". What code do i need to fix this!

hope someone can help, sorry for the poor explanation, im tired!! thanks for any help if i get any from chit chat :D
 
You can use isset() and empty() php functions to check if and what data was typed by user and then build your query dynamicly.

Here's just a quick example:
PHP:
// just to have anything to work with
$ffrom = 'test ffrom';
$fto = 'test ftp';
$flight_no = 'test flight';
$arrival = 'test arrival'; 

// example script
$cond = array();
if(!empty($ffrom)) $cond[] = "ffrom = '".$ffrom."'";
if(!empty($fto)) $cond[] = "fto = '".$fto."'";
if(!empty($flight_no)) $cond[] = "flight_no = '".$flight_no."'";
if(!empty($departure)) $cond[] = "departure = '".$departure."'";
if(!empty($arrival)) $cond[] = "arrival = '".$arrival."'";
$query = "SELECT * FROM airline WHERE ". implode(' AND ', $cond);

Above code should generate this:
Code:
SELECT * FROM airline WHERE ffrom = 'test ffrom' AND fto = 'test ftp' AND flight_no = 'test flight' AND arrival = 'test arrival'

I didn't put any data into $departure for the purpose. It's empty so it wont be included into query.
 
Last edited:
:D Thanks a lot for that Brajan, worked a treat :D

Any ideas why i might get this and how to fix it?

Error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\webroot\flights.php on line 93

page still runs and does the searches fine tho, but thats what i get when the page first loads with the results returned.

code i used is:

For ($i = 0; $i < mysql_num_rows($result); $i++)
{
$ffrom = mysql_result($result, $i, "ffrom");
$fto = mysql_result($result, $i, "fto");
$flight_no = mysql_result($result, $i, "flight_no");
$departure = mysql_result($result, $i, "departure");
$arrival = mysql_result($result, $i, "arrival");

Thanks!!
 
Looks like "mysql_num_rows($result)" returning nothing (if none records found)
Try to print it on screen. If it'll print nothing ... there is your problem.
Try this:
PHP:
if(@mysql_num_rows($result) > 0){


for ($i = 0; $i < mysql_num_rows($result); $i++)
{
....
}
}
Imo you always should check "mysql_num_rows($result)" value before you go into ANY loop. At least this is how i'm doing it :)

EDIT:
Oh one more. If you don't know it yet '@' right before some functions disabling error messages. Use it carefully ;)
 
Last edited: