Jump to content

Archived

This topic is now archived and is closed to further replies.

puja

search query

Recommended Posts

hi
im trying to do a search query for booking accommodation but it isnt working!
it lets me view the page but it says:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\APACHE2\Apache2\htdocs\search.php on line 30
No Record Found


so the query is obviously wrong, i dont know what i am doing wrong tho. Line 30 is the one in bold

my query looks like:

$accommID = $_POST["accommID"];
$type = $_POST["select_type"];
$sleeps = $_POST["select_sleeps"];
$board = $_POST["select_board"];
$description = $_POST["description"];
$pets_allowed = $_POST["select_pets_allowed"];

$XX = "No Record Found";

$query = mysql_query("SELECT * FROM accommodation WHERE $type = 'select_type' && $sleeps = 'select_sleeps' && $board = 'select_board' && $pets_allowed = 'select_pets_allowed'");
[b]while ($row = mysql_fetch_array($query)) [/b]
{
$type=$row["select_type"];
$sleeps=$row["select_sleeps"];
$board=$row["select_board"];
$pets_allowed=$row["select_pets_allowed"];

print ("this is for $type, and $sleeps and $board and $pets_allowed");
}

//below this is the function for no record!!
if (!$type)
{
print ("$XX");
}

i hope someone can understand wot im trying to do with it because im not sure i do anymore! this is going on my search page but i dont where the result shud be shown as the options are chosen on this page and so shud it go to another page to show the results?

Share this post


Link to post
Share on other sites
your first part might be the culprit:

$query = "SELECT * FROM accommodation WHERE ";
mysql_query($query, $connection) or die (mysql_error());

it appears to be unnecessary. If it is necessary, remove the "WHERE" from your query and it should be happy.

Also, when you're echoing your mysql_error() you might add a line letting you know which query went wrong. For example:

mysql_query($query, $connection) or die ("First query ".mysql_error());

Share this post


Link to post
Share on other sites
in your mysql query you have used variables instead of field names.

instead of "WHERE $variable = $variable && $another_variable = $something_else" you should have "WHERE field_1 = $variable && field_2 = $another_variable"

Hope this helps.

Share this post


Link to post
Share on other sites
hey guys
that does help
thanks i changed that query around and i am still getting an error with that it says:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\APACHE2\Apache2\htdocs\search.php on line 30

maybe its because i aint doing nething with the booking start and end dates.
could anyone tell me if i am even approaching it the rite way.
i am trying to obviously do a holiday booking system. this page i am trying to do should allow the customers to specify the accommodation they want and then the dates and it should give them the results if there is any accommodation for the dates they have chosen.
At the momement i aint storing the dates newhere so it shud b available for any dates they chose rite?
so the results shud show on a separate page rite?

Share this post


Link to post
Share on other sites
oh sorry, i shud have clarified that.
its the same as before, the line in bold in the code, its the line with the while loop

Share this post


Link to post
Share on other sites
Try using this...

[code]
$query = mysql_query("SELECT * FROM accommodation WHERE $type = 'select_type' && $sleeps = 'select_sleeps' && $board = 'select_board' && $pets_allowed = 'select_pets_allowed'");

$x=0;
while ($x<mysql_numrows($query));
{
$row = mysql_fetch_array($query);
type=$row["select_type"];
$sleeps=$row["select_sleeps"];
$board=$row["select_board"];
$pets_allowed=$row["select_pets_allowed"];

print ("this is for $type, and $sleeps and $board and $pets_allowed");

$x++;
}

//below this is the function for no record!!
if (!$type)
{
print ("$XX");
}
[/code]

(sorry... there was an error that i just fixed)

Share this post


Link to post
Share on other sites
hi
thanks for trying but that still doesnt work.

my code now looks like:

[code]

<html>
<head>
<title><?php echo "Search" ?></title>
<link rel="stylesheet" type="text/css" href="template2.css">
<head>
<body>

<?
require_once("config.php");
     $connection = @mysql_connect($db_host, $db_user, $db_password) or die("oops! error connecting");
    
     mysql_select_db($db_name, $connection);


$self = $_SERVER['PHP_SELF'];



$accommID = $_POST["accommID"];
$type = $_POST["select_type"];
$sleeps = $_POST["select_sleeps"];
$board = $_POST["select_board"];
$description = $_POST["description"];
$pets_allowed = $_POST["select_pets_allowed"];


$XX = "No Record Found";

$query = mysql_query("SELECT * FROM accommodation WHERE select_type = '$type' AND select_sleeps = '$sleeps' AND select_board = '$board' AND select_pets_allowed = '$pets_allowed'");  
while ($row = mysql_fetch_array($query))
{
$type=$row["select_type"];
$sleeps=$row["select_sleeps"];
$board=$row["select_board"];
$pets_allowed=$row["select_pets_allowed"];

print ("this is for $type, and $sleeps and $board and $pets_allowed");
}

//below this is the function for no record!!
if (!$type)
{
print ("$XX");
}
//end



//end

?>

<table border="1" width="100%" height="226">
  <tr>
    <td width="22%" height="91" bgcolor="#0099FF">&nbsp;
    <img alt ="[Company Logo]"
    src="file:///C:/Documents%20and%20Settings/Puja/My%20Documents/My%20Pictures/puj2.gif" width="100" height="100">
    <p>
    </td>
    <td width="78%" height="91" bgcolor="#66CCFF" ALIGN=CENTER>&nbsp;

<H1> Search </H1>

    </td>
  </tr>
  <tr>
    <td width="22%" height="123" bgcolor="#6699FF">&nbsp;
      <div class="buttonscontainer">
      <div class="buttons">
      <a href="gen_homepage.php">Homepage</a>
      <a href="register.php">Register</a>
      <a href="calendar.php">Calendar</a>
      <a href="weatherinfo.php">Weather Information</a>
      <a href="travelinfo.php">Travel Information</a>
      </div>
      </div>
    </td>
    <td width="78%" height="123" bgcolor="#99CCFF" ALIGN=CENTER>
     &nbsp;
          &nbsp;
          &nbsp;
<center>
    <form name="search.php" action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST">
          <font face="arial" size ="1">
    Type:
    <select name="select_type">
            <option value='Villa'>Villa</option>
            <option value='Caravan'>Caravan</option>
            <option value='Lodge'>Lodge</option>
          </select>
          &nbsp;
          Sleeps:
          <select name="select_sleeps">
            <option value='2'>Up to 2</option>
            <option value='4'>Up to 4</option>
            <option value='6'>Up to 6</option>
            <option value='8'>Up to 8</option>
            <option value='10'>Up to 10</option>
          </select> &nbsp; <br> <br>
          Board:
          <select name="select_board">
            <option value='Full Board'>Full Board</option>
            <option value='Half Board'>Half Board</option>
            <option value='B & B'>B & B</option>
            <option value='Self-Catering'>Self-Catering</option>
          </select> &nbsp;
          Pets Allowed:  
          <select name="select_pets_allowed">
            <option value='Y'>Yes</option>
            <option value='N'>No</option>
          </select> &nbsp;
          <br> <br>

           Start Date: <input type="text" name ="booking_start_date"> &nbsp;
           End Date: <input type="text" name ="booking_end_date"> &nbsp;
          
           <center> <input type="submit" value="Search"></center>

  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>

  </form>
</td>



</table>
  

</body>
[/code]

this i think searches it correctly because when i put in correct details it doesnt say No Record Found but it does still give me the error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\APACHE2\Apache2\htdocs\search.php on line 30

which is still that same line with the while loop.

also i havnt got newhere to display the results or tell it to go to another page to display the results, but that print line ive put in should print what they have searched for shudnt it?

Share this post


Link to post
Share on other sites
You have: while ($row = mysql_fetch_array($query)) { // Do whatever }

But... $row hasn't been specified anywhere, so doesnt exist yet, or at least has an value of "" (nothing).

you should have:
$row = mysql_fetch_array($query);

which will enable you to use the $row[db_field_name] style variables... then you need the while loop... but you need to specify something for the while loop to work with, such as:

$x=0;
while($x<mysql_numrows($query))
{
// Do something
$x++;
}


Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.