Help with php search function

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
FatC
Forum Newbie
Posts: 4
Joined: Tue Dec 29, 2009 10:21 am

Help with php search function

Post by FatC »

New to php. I've setup a search for serial number for a part number database. I thought it was working but i noticed when you enter "DV441" it returns a blank page....no error or anything. Seems like i have that problem with any part number that starts with DV.....the other seem to work. Example: DA411.

Link: http://www.brodix.com/numbers/numbersearch.php

Code:

Code: Select all

<?php
$dbc = mysql_connect ('localhost', 'name', 'password')
	or die('Error connecting to MySQL server.'); 
mysql_select_db('database', $dbc)
	or die('db select fail.');
	
$number = mysql_real_escape_string ($_POST["number"], $dbc)
or die ("Please enter a BRODIX serial number");

 $sql = "SELECT * FROM numbers WHERE number = '$number'"
   or die("We can't find a match for your Serial Number in our records. Please call and talk to our tech department 479-394-1075 to indentify your part. Thanks");  

      $result = mysql_query($sql)
      or die("result error"); 
      $num = mysql_num_rows($result)
      or die("We can't find a match for your Serial Number in our records. Please call and talk to our tech department 479-394-1075 to indentify your part. Thanks");
$i=1;
if ($numm = mysql_fetch_row($result))
	{
	while ($i < $num) {
	$id=mysql_result($result,$i);
	$date_cast = mysql_result($result,$i,"date_cast");
	$number = mysql_result($result,$i,"number");
	$description = mysql_result($result,$i,"description");
?>	
	<font face="Arial, Helvetica, sans-serif" size="1">

	Your part was cast on: <?php echo $date_cast ?>
	<br />
	The serial number is: <?php echo $number ?>
	<br />
	Your part is: <?php echo $description ?>
	<br />
	If you have any other questions please call our tech department at 479-394-1075.
	<br />
	Thanks
	<br />
	<br />
				
	<?php
	$i++;
	}
        }
	mysql_close();
	?>

Any ideas? Thanks
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Help with php search function

Post by mikosiko »

In this piece of code

Code: Select all

      
     $num = mysql_num_rows($result)
      or die("We can't find a match for your Serial Number in our records. Please call and talk to our tech department 479-394-1075 to indentify your part. Thanks");

$i=1;

if ($numm = mysql_fetch_row($result))
        {
        while ($i < $num) {
if your select return only 1 record (then $num = 1) the while loop will not execute because $i will be equal to $num... is that what you want?

also.. why are you using the form "if... while"... if you can use directly the while loop and access your column values from the array $numm ?.... (I understand that that could be just a matter of programing preference... but looks curious to me)
FatC
Forum Newbie
Posts: 4
Joined: Tue Dec 29, 2009 10:21 am

Re: Help with php search function

Post by FatC »

Wow....u would think i would have seen that. Changed it, tested it and it works! Thanks for the help.

And about the "if and while" function. I used the php code from another function on the website and just kept the if and while functions. I'll change that also.

Thanks again for your help!
Post Reply