Search Function Problem

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
Arsenal Rule
Forum Commoner
Posts: 26
Joined: Fri Apr 08, 2005 4:13 am

Search Function Problem

Post by Arsenal Rule »

Basically I have tried creating another search function that will search the VAT registration number "vat_registration_number" from my "vat" table and I have stumbled across another problem yet again. I have created this code for this search below.

Code: Select all

<form name="search" action=<?php Print $_SERVER["PHP_SELF"]; ?> method="post">
            <br>
			 <hr width=100%>
            <?php

			$Order = (isset($_POST['Order']))?$_POST['Order']:"tax_id";
			$InOrder = (isset($_POST['InOrder']))?$_POST['InOrder']:"";
			$vatreg = (isset($_POST['vatreg']))?$_POST['vatreg']:"";

			?>
            <strong><br>
            </strong>
            <table align=center>
                  <th align=center><span class="style7">Search For VAT Registration Number: </span>
                    <td><input type ="text" name="vatreg" value ="<?print $vatreg;?>"size="40">
            </table>
            <br>
            
			
			<table align=center>
            	<td> <span class="style13">Make Search Found Ascending Or Descending:</span>            	  
				<select name="InOrder">
               		<option value="ASC"<?if ($InOrder == "ASC"){print ' SELECTED ';}?>>Ascending</option>
               		<option value="DESC"<?if ($InOrder == "DESC"){print ' SELECTED ';}?>>Descending</option>
               	  </select>
           	</table>
			<br>
            <table align=center>
            <th align=center>
              <td><input type="submit" name="submit" value="Search" />
            </table>
			<br>
			
            <hr width=100%>			
            
            <?

		//May not be needed depending on the connection.

			$conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
				mysql_select_db("aziz",$conn) or die("Could Not Select The Database");

		//build up query using each conditional - else statement refers to search field.

			$conditional = "";

			if ($vatreg ==""){
			$conditional=$conditional."vat_registration_number = '".$vatreg."'";
			}
			
			$conditional="WHERE ".substr($conditional,0,strlen($conditional)-4);

				$query = "SELECT vat_id, c_registration_no, vat_registration_number, vat_effective_date, vat_quarter FROM vat WHERE vat_registration_number = '$vatreg' ORDER BY $Order $InOrder";

				$search = mysql_query($query,$conn);

				$NumRows=mysql_num_rows($search);
		?>
            <center>
              <b></strong><?print "There are $NumRows found items related to your search ";?></b>
              <span class="style3"><span class="style3 style5"></span></span>            
            </center>
            <br>
			 <table border="0" cellpadding="0" cellspacing="0" align="center">
          <tr height="20">
            <td bgcolor="#93B0B4" width="145"><span class="style4 style13">VAT ID </span></td>
            <td bgcolor="#93B0B4" width="145"><span class="style4 style13">Company Registration Number</span></td>
            <td bgcolor="#93B0B4" width="145"><span class="style4 style13">VAT Registration Number</span></td>
			<td bgcolor="#93B0B4" width="145"><span class="style4 style13">VAT Effective Date (YYYY-MM-DD)</span></td>
			<td bgcolor="#93B0B4" width="145"><span class="style4 style13">VAT Quarter</span></td>
			<td bgcolor="#93B0B4" width="125"><span class="style4 style13">Delete Client</span></td>
          </tr>
          <tr>
            <td>
            <?php
	if($NumRows>0){

	//get the first row and begin the table.

	$row = mysql_fetch_assoc ($search);

			//reset the result set back to the first row and display the data set.
			mysql_data_seek ($search, 0);
			while ($row = mysql_fetch_row ($search)) {
			
			echo "<tr valign=top>";
			echo "<td bgcolor=#93B0B4 colspan=6></td>";
			echo "</tr>";
			
			print "<TR>\n";
			print "<TD>$row[0]</TD>";
			print "<TD>$row[1]</TD>";
			print "<TD>$row[2]</TD>";
			print "<TD>$row[3]</TD>";
			print "<TD>$row[4]</TD>";
			if($row[2]=="vatreg"){
			
			?>
			</td>
          </tr>
        </table>
            <?php
			print $row[0];
			?>
            <?php
			print "</TR>";
			
   		 	}
			else{
			
			?>
          </form>
        </blockquote>
    <td><a href ="viewvat2.php?id=
			<?php
			print $row[0];
			?>
			">View</a></td>
    <?php
			print "</tr>";
			}
			}
			print "</table>";
			}
			
?>
When I run this it comes up with this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\apachefriends\xampp\htdocs\searchvatreg.php on line 183
which line 183 is this part of the code:

Code: Select all

$NumRows=mysql_num_rows($search);
which is connected to this part of the code:

Code: Select all

$conditional = "";

			if ($vatreg ==""){
			$conditional=$conditional."vat_registration_number = '".$vatreg."'";
			}
			
			$conditional="WHERE ".substr($conditional,0,strlen($conditional)-4);

				$query = "SELECT vat_id, c_registration_no, vat_registration_number, vat_effective_date, vat_quarter FROM vat WHERE vat_registration_number = '$vatreg' ORDER BY $Order $InOrder";

				$search = mysql_query($query,$conn);

				$NumRows=mysql_num_rows($search);
It maybe something small I'm missing I have tried so many different ways in solving this problem but nothing has worked so far. I hope someone can help me to figure out a solution to help me solve this problem because I have tried everything I can do with my basic knowledge of PHP.

I really hope someone can help me with this problem I would be really greatfull

Thankyou for your time

Take Care
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

have you tried to echo out search to see what you query actually is?
Arsenal Rule
Forum Commoner
Posts: 26
Joined: Fri Apr 08, 2005 4:13 am

Search Function Problem

Post by Arsenal Rule »

Thank you mate for replying back :D I did what you said and it revealed to me what the problem was which is great and I sorted it out and now it is working thank you mate for you help to solve this problem thank you mate again really appreciate it :D

Thank You :D

TAKE CARE MATE :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you should alwasy have an or die() statement after each query. It will tell you exactly what the problem is.

for example

Code: Select all

$result = mysql_query($sql) or die(mysql_error())
edit: 2000 posts ;)
Post Reply