...

View Full Version : Having some trouble with this query...



chornbeck
02-07-2006, 09:07 PM
Need a little noob help here.. I'm running this query which should search my MySQL DB and return only the records that have the same last name and a first name that begins with the first initial that the person enters. It works fine to look up the last name, but when I try to add on the first initial, it craps out. I'm having a problem with the proper wildcards, I believe, but maybe my PHP is flawed as well.. Take a look if you would...


<?
if($_POST['submit'])
{
mysql_connect("localhost","orlandoi_referra","referral2");
mysql_select_db("orlandoi_referralowners") or die ('I cannot connect to the database because: ' . mysql_error());

$LstName = make_safe($_POST['LstName']);
$FirstInitial = make_safe($_POST['FirstInitial']);




$result = mysql_query("SELECT * FROM owner_data WHERE LastName = '$LstName' AND FirstName like '% FirstInitial%% %'") or die(mysql_error());
?>

<table width="600" border="1" cellspacing="0" cellpadding="2">
<tr>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Zip</th>
<th scope="col">OwnerID</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo " ".$row['FirstName'];?></td>
<td><?php echo " ".$row['LastName'];?></td>
<td><?php echo " ".$row['PostalCode'];?></td>
<td bgcolor="#fffc00"><b><?php echo " ".$row['OwnerID'];?></b></td>
</tr>
<?php
}
}
function make_safe($variable)
{
$variable = addslashes(trim($variable));
return $variable;
}

?>

Kid Charming
02-07-2006, 09:28 PM
You've got too many wildcards. Try:



SELECT
*
FROM
owner_data
WHERE
LastName = '$LstName'
AND
FirstName LIKE '$FirstInitial%'

vinyl-junkie
02-09-2006, 02:36 AM
Slight error, Kid Charming. ;)


SELECT
*
FROM
owner_data
WHERE
LastName = '$LstName'
AND
FirstName LIKE '%$FirstInitial%'

Kid Charming
02-09-2006, 02:47 AM
That depends. ;) If $firstInitial has to be the first character in the field's value, you don't want a wildcard in front of it. Otherwise, your select will retrieve all rows that contain $firstInitial anywhere.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum