php search form to mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
olm75
Forum Newbie
Posts: 16
Joined: Fri Aug 05, 2005 8:05 pm

php search form to mysql database

Post by olm75 »

ok i'm getting an error message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/nightsp/public_html/testcode.php on line 38

Sorry, No Records Were Found!

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home2/nightsp/public_html/testcode.php on line 46


and here is the code can you read the notes i put in please thanks, i greatly appreciate this:
by the way $AG = $_POST[agegroup], $VT = $_POST[venuetype]
$MT = $_POST[musictype]; are list boxes as u can see on the page nightspotz.com

Code: Select all

<?php
$VN = $_POST[venuename];
$AC = $_POST[areacode];
$AG = $_POST[agegroup];

//-----HOW DO I ADD THESE TO VARIBLES TO THE QUERY FROM MY SEARCH FORM---------
$VT = $_POST[venuetype];
$MT = $_POST[musictype];
//-----------------------------------------------------
  
$HostID = $_POST[localhost];
$UserID = $_POST[*******]; 
$PassID = $_POST[*******];
$dbname = $_POST[********];
$Table = $_POST[venues];


//----------CAN I TAKE THIS TABLE OUT OR WAS THIS JUST TO ECHO AND SEE IF IT WORKS..I DONT NEED ANY OF
//---THESE TABLES I'LL FORMAT IT IN DREAMWEAVER BUT CAN U SHOW ME HOW TO JUST GET THE RESULTS WITH THE
//----PHP ICON SO I CAN JUST POSITION THEM IN DREAMWEAVER...THANKS...
echo "<TABLE BORDER=1 align=center>";
echo "<TD>First Name</TD><TD>Last Name</TD><TD>Street Address</TD><TD>City</TD><TD>State</TD><TR>";
echo "<td>".$VN."</td><td>".$AC."</td><td>".$AG."</td>";
echo "<td>".$City."</td><td>".$State."</td>";
echo "</TR></table><p>";
//---------------------------------------------------------------------------------------------

mysql_select_db ($dbname);
$mysql_mylink = mysql_pconnect($HostID, $UserID, $PassID);
echo "<TABLE class='sortable' id='Identify' BORDER=1 align=center>";
echo "<TD>VENUENAME</TD><TD>AREACODE</TD><TD>Street Address</TD><TD>city</TD><TD>state</TD>";
$querystring = "select * from '$Table'";
if ($VN != "" && $AC != "" && $AG != "") {$querystring = $guerystring + "where (venuename like '%$VN%') and (areacode like '%$AC%') and (agegroup like '%$AG%')";}
if ($VN != "" && $AC != "" && $AG == "") {$querystring = $guerystring + "where (venuename like '%$VN%') and (areacode like '%$AC%')";}
if ($VN != "" && $AC == "" && $AG == "") {$querystring = $guerystring + "where (venuename like '%$VN%')";}
if ($VN != "" && $AC == "" && $AG != "") {$querystring = $guerystring + "where (venuename like '%$VN%') and (agegroup like '%$AG%')";}
$SearchMatch = mysql_query ($querystring);
if ($MatchRecordRow = mysql_fetch_array($SearchMatch)) {
   do{
      echo "<TR><td>".$MatchRecordRow['venuename']."</TD>";
      echo "<td>".$MatchRecordRow['areacode']."</TD><td>".$MatchRecordRow['agegroup']."</TD>";
      echo "<td>".$MatchRecordRow['city']."</TD><td>".$MatchRecordRow['state']."</TD>";
} while($MatchRecordRow = mysql_fetch_array($SearchMatch));
} else {echo "<center>Sorry, No Records Were Found!</center>";}
echo "</table>";
mysql_free_result($SearchMatch);
?>
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Try adding

Code: Select all

or die(mysql_error());
after just about all your mysql_something calls.

That will tell you where the actual problem is occurring. I think you might have the mysql_select_db and mysql_pconnect out of order (flipping them might fix it) but the or die(mysql_error()) should help you find the real problem.
olm75
Forum Newbie
Posts: 16
Joined: Fri Aug 05, 2005 8:05 pm

sorry

Post by olm75 »

that didnt work any other suggestions....
these are the lines that have the errors.....

line 38:
if ($MatchRecordRow = mysql_fetch_array($SearchMatch)) {

line 46:
mysql_free_result($SearchMatch);
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: php search form to mysql database

Post by nielsene »

That error means you have another error before. That's why I suggested you add the "or die(...)s" as shown below. THis should identify where the orginal error is occuring....

Code: Select all

<?php
$VN = $_POST[venuename];
$AC = $_POST[areacode];
$AG = $_POST[agegroup];

//-----HOW DO I ADD THESE TO VARIBLES TO THE QUERY FROM MY SEARCH FORM---------
$VT = $_POST[venuetype];
$MT = $_POST[musictype];
//-----------------------------------------------------
  
$HostID = $_POST[localhost];
$UserID = $_POST[*******]; 
$PassID = $_POST[*******];
$dbname = $_POST[********];
$Table = $_POST[venues];


//----------CAN I TAKE THIS TABLE OUT OR WAS THIS JUST TO ECHO AND SEE IF IT WORKS..I DONT NEED ANY OF
//---THESE TABLES I'LL FORMAT IT IN DREAMWEAVER BUT CAN <span style='color:blue' title='ignorance is bliss'>you</span> SHOW ME HOW TO JUST GET THE RESULTS WITH THE
//----PHP ICON SO I CAN JUST POSITION THEM IN DREAMWEAVER...THANKS...
echo "<TABLE BORDER=1 align=center>";
echo "<TD>First Name</TD><TD>Last Name</TD><TD>Street Address</TD><TD>City</TD><TD>State</TD><TR>";
echo "<td>".$VN."</td><td>".$AC."</td><td>".$AG."</td>";
echo "<td>".$City."</td><td>".$State."</td>";
echo "</TR></table><p>";
//---------------------------------------------------------------------------------------------

mysql_select_db ($dbname) or die(mysql_error());
$mysql_mylink = mysql_pconnect($HostID, $UserID, $PassID) or die(mysql_error());
echo "<TABLE class='sortable' id='Identify' BORDER=1 align=center>";
echo "<TD>VENUENAME</TD><TD>AREACODE</TD><TD>Street Address</TD><TD>city</TD><TD>state</TD>";
$querystring = "select * from '$Table'";
if ($VN != "" && $AC != "" && $AG != "") {$querystring = $guerystring + "where (venuename like '%$VN%') and (areacode like '%$AC%') and (agegroup like '%$AG%')";}
if ($VN != "" && $AC != "" && $AG == "") {$querystring = $guerystring + "where (venuename like '%$VN%') and (areacode like '%$AC%')";}
if ($VN != "" && $AC == "" && $AG == "") {$querystring = $guerystring + "where (venuename like '%$VN%')";}
if ($VN != "" && $AC == "" && $AG != "") {$querystring = $guerystring + "where (venuename like '%$VN%') and (agegroup like '%$AG%')";}
$SearchMatch = mysql_query ($querystring) or die(mysql_error());
if ($MatchRecordRow = mysql_fetch_array($SearchMatch)) {
   do{
      echo "<TR><td>".$MatchRecordRow['venuename']."</TD>";
      echo "<td>".$MatchRecordRow['areacode']."</TD><td>".$MatchRecordRow['agegroup']."</TD>";
      echo "<td>".$MatchRecordRow['city']."</TD><td>".$MatchRecordRow['state']."</TD>";
} while($MatchRecordRow = mysql_fetch_array($SearchMatch));
} else {echo "<center>Sorry, No Records Were Found!</center>";}
echo "</table>";
mysql_free_result($SearchMatch);
?>
ckuipers
Forum Commoner
Posts: 61
Joined: Mon Mar 24, 2003 6:10 am

Post by ckuipers »

Try making it

Code: Select all

$SearchMatch = mysql_query ($querystring, $mysql_mylink);
olm75
Forum Newbie
Posts: 16
Joined: Fri Aug 05, 2005 8:05 pm

thanks guys, but

Post by olm75 »

ok now the test worked but now the the variables arent working because i cant do any type of search i can only hit submit but i think the SQL qyery is jacked up can u guys help with this i want the viewers to be able to search by using 1 or all of the search fields to narrow the search down.....
here is the working form...
http://www.nightspotz.com/testsearch.php
can u go and check it out
oh by the way the mistake was
$querystring = "select * from '$Table'";
had to get rid of the ' ' around the $Table.....
can u help with the rest please...u dont know how much of a relief this is...
Post Reply