Search returning empty results

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
jstone3503
Forum Newbie
Posts: 3
Joined: Sat May 16, 2009 9:20 am

Search returning empty results

Post by jstone3503 »

I am trying to have a simple motor vehicle registration search page where you type in a tag and it returns the basic vehicle information for a school to use. I got everything done but when I search, it returns the table I have made with the headings and so forth, but nothing is being pulled from the database. It's been several years since I've worked with PHP and probably is just one small thing I am missing, but here is my code.

Code: Select all

<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("shb12_041_2", $con);
$sql=("SELECT * FROM mvr WHERE plate = $_POST['plate']");
$result=mysql_query($sql) or die("Error in selecting the database:".mysql_error());
$row=mysql_fetch_row($result);
 
$ownername = $row['ownername'];
$plate = $row['plate'];
$expiration = $row['expiration'];
$address = $row['address'];
$citystatezip = $row['citystatezip'];
$bodytype = $row['bodytype'];
$year = $row['year'];
$make = $row['make'];
$model = $row['model'];
 
echo "<table border=1>";
echo "<tr><td align=top><small>Owner Name</small><br>$ownername</td><td align=top><small>Current Plate</small><br>$plate</td><td align=top><small>Expiration</small><br>$expiration</td><td align=top><small>Address</small><br>$address</td></tr>";
echo "<tr><td align=top><small>City/State/Zip</small><br>$citystatezip</td><td align=top><small>Body Type</small><br>$bodytype</td><td align=top><small>Year</small><br>$year</td><td align=top><small>Make</small><br>$make</td></tr>";
echo "<tr><td align=top><small>Model</small><br>$model</td><td align=top><small>Color</small><br>$color</td><td align=top><small>Stolen</small><br>$stolen</td><td align=top></td></tr>";
 
mysql_close($con);
?> 
 
Last edited by Benjamin on Sat May 16, 2009 10:21 am, edited 1 time in total.
Reason: Changed code type from text to php.
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: Search returning empty results

Post by ldougherty »

try this..

Code: Select all

 
<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("shb12_041_2", $con);
$sql=("SELECT * FROM mvr WHERE plate = $_POST['plate']");
$result=mysql_query($sql) or die("Error in selecting the database:".mysql_error());
 
while ($row = mysql_fetch_array($result)) {
 
 
$ownername = $row['ownername'];
$plate = $row['plate'];
$expiration = $row['expiration'];
$address = $row['address'];
$citystatezip = $row['citystatezip'];
$bodytype = $row['bodytype'];
$year = $row['year'];
$make = $row['make'];
$model = $row['model'];
 
echo "<table border=1>";
echo "<tr><td align=top><small>Owner Name</small><br>$ownername</td><td align=top><small>Current Plate</small><br>$plate</td><td align=top><small>Expiration</small><br>$expiration</td><td align=top><small>Address</small><br>$address</td></tr>";
echo "<tr><td align=top><small>City/State/Zip</small><br>$citystatezip</td><td align=top><small>Body Type</small><br>$bodytype</td><td align=top><small>Year</small><br>$year</td><td align=top><small>Make</small><br>$make</td></tr>";
echo "<tr><td align=top><small>Model</small><br>$model</td><td align=top><small>Color</small><br>$color</td><td align=top><small>Stolen</small><br>$stolen</td><td align=top></td></tr>";
echo "</table>";
echo "<br><br>";
 
}
 
mysql_close($con);
?>
 
Last edited by Benjamin on Sat May 16, 2009 1:05 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: Search returning empty results

Post by Defiline »

Code: Select all

<?php
 
$con = @mysql_connect("host","username","password");
 
if(!$con) {
  die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("shb12_041_2", $con);
 
 
/**
 * Constructing ...
 */
$sql    = "SELECT * FROM `mvr` WHERE `plate` = '".$_POST['plate']."'";
$result = mysql_query($sql);
 
echo '<pre>';
print_r(mysql_fetch_row($result));
 
while($row = mysql_fetch_assoc($result)) {
    
    // While ...
    
}
jstone3503
Forum Newbie
Posts: 3
Joined: Sat May 16, 2009 9:20 am

Re: Search returning empty results

Post by jstone3503 »

I couldn't get ldougherty's code to work but I got the other and wasn't exactly what I was looking for but at least it shows the results. But instead of being in a table it shows up like so:

Array
(
[0] => JOHN DOE
[1] => CA
[2] => ABC123
[3] => 2009-03-31
[4] => 1234 MAIN ST
[5] => BEVERLY HILLS, CA 90210
[6] => SUV
[7] => 2008
[8] => FORD
[9] => MUST
[10] => YEL
[11] => N
[12] =>
)

Is there something else missing that would cause a reason for the table not to be displayed, even though its an array?
ldougherty
Forum Contributor
Posts: 103
Joined: Sun May 03, 2009 11:39 am

Re: Search returning empty results

Post by ldougherty »

What happens when you run my code? Without the database I can't exactly replicate it but I thought the syntax was on.

If you want to create a dump of your database I'll set it up locally and test it to get it working properly.
jstone3503
Forum Newbie
Posts: 3
Joined: Sat May 16, 2009 9:20 am

Re: Search returning empty results

Post by jstone3503 »

nevermind. i realized it was a mistake on my end. it is working perfect now. thanks for all the help guys.
Post Reply