coxdabd
January 11, 2011, 10:52am
1
Hi all, I’m working on a search function on my site, thought it would be straight forward but can’t work out why it’s only returning one row when it should return two, I know it should as I have searched in PHPMyAdmin.
Had a browse about on Google and think it may be because I’m using mysql_fetch_array. Tried using mysql_num_rows and that seems to put the thing in an infinite loop so I’m stumped now? I have tried turning on error reporting and got a few warnings over unidentified variables but that’s now all sorted.
$search = mysql_query("SELECT * FROM `customers` WHERE `c_address` LIKE '%tq12%'");
while ($row = mysql_fetch_array($search)){
$searchRows = '<div class="searchRes">
<span class="searchRow" style="width: 65px;">'.$row['c_title'].' '.$row['c_firstname'].' '.$row['c_lastname'].'</span>
<span class="searchAddress" style="width: 100px;">'.str_replace(",","<br/>", $row['c_address']).'</span>
<span class="searchRow">'.$row['c_number'].'</span>
<span class="searchRow">'.$row['c_email'].'</span>
<span class="searchRow">'.$row['c_dob'].'</span>
<span class="searchRow">'.$row['c_regdate'].'</span>
<span class="searchRow">'.$row['c_notes'].'</span>
</div>';
}
Unless you’re using $searchRows elsewhere, this will suffice.
$search = mysql_query("SELECT * FROM `customers` WHERE `c_address` LIKE '%tq12%'");
while ($row = mysql_fetch_array($search)){
echo '<div class="searchRes">
<span class="searchRow" style="width: 65px;">'.$row['c_title'].' '.$row['c_firstname'].' '.$row['c_lastname'].'</span>
<span class="searchAddress" style="width: 100px;">'.str_replace(",","<br/>", $row['c_address']).'</span>
<span class="searchRow">'.$row['c_number'].'</span>
<span class="searchRow">'.$row['c_email'].'</span>
<span class="searchRow">'.$row['c_dob'].'</span>
<span class="searchRow">'.$row['c_regdate'].'</span>
<span class="searchRow">'.$row['c_notes'].'</span>
</div>';
}
coxdabd
January 11, 2011, 11:01am
3
AnthonySterling:
Unless you’re using $searchRows elsewhere, this will suffice.
$search = mysql_query("SELECT * FROM `customers` WHERE `c_address` LIKE '%tq12%'");
while ($row = mysql_fetch_array($search)){
echo '<div class="searchRes">
<span class="searchRow" style="width: 65px;">'.$row['c_title'].' '.$row['c_firstname'].' '.$row['c_lastname'].'</span>
<span class="searchAddress" style="width: 100px;">'.str_replace(",","<br/>", $row['c_address']).'</span>
<span class="searchRow">'.$row['c_number'].'</span>
<span class="searchRow">'.$row['c_email'].'</span>
<span class="searchRow">'.$row['c_dob'].'</span>
<span class="searchRow">'.$row['c_regdate'].'</span>
<span class="searchRow">'.$row['c_notes'].'</span>
</div>';
}
Hi Anthony, thanks for your quick response. I was trying to store the the while in a variable and echo it further down the page hence I had the same variable twice. All sorted now, thank you. I’ve just moved the script itself further down the page. Cheers dude
Have you thought above creating a function for this? You can the just call it where ever you need it; take this very rough example.
function search($table, $field, $like){
$rows = array();
$sql = sprintf(
"SELECT * FROM `%s` WHERE `%s` LIKE '%%%s%%';",
$table,
$field,
mysql_real_escape_string($like)
);
$res = mysql_query($sql);
if(is_resource($res)){
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
}
return $rows;
}
<?php foreach(search('customers', 'c_address', 'tq12') as $row): ?>
<div class="searchRes">
<span class="searchRow"><?php echo $row['c_number']; ?></span>
<span class="searchRow"><?php echo $row['c_email']; ?></span>
<span class="searchRow"><?php echo $row['c_dob']; ?></span>
<span class="searchRow"><?php echo $row['c_regdate']; ?></span>
<span class="searchRow"><?php echo $row['c_notes']; ?></span>
</div>
<?php endforeach; ?>
It allows for a little cleaner code too. IMO.
AnthonySterling:
Have you thought above creating a function for this? You can the just call it where ever you need it; take this very rough example.
function search($table, $field, $like){
$rows = array();
$sql = sprintf(
"SELECT * FROM `%s` WHERE `%s` LIKE '%%%s%%';",
$table,
$field,
mysql_real_escape_string($like)
);
$res = mysql_query($sql);
if(is_resource($res)){
while($row = mysql_fetch_assoc($res)){
array_push($rows, $row);
}
}
return $rows;
}
<?php foreach(search('customers', 'c_address', 'tq12') as $row): ?>
<div class="searchRes">
<span class="searchRow"><?php echo $row['c_number']; ?></span>
<span class="searchRow"><?php echo $row['c_email']; ?></span>
<span class="searchRow"><?php echo $row['c_dob']; ?></span>
<span class="searchRow"><?php echo $row['c_regdate']; ?></span>
<span class="searchRow"><?php echo $row['c_notes']; ?></span>
</div>
<?php endforeach; ?>
It allows for a little cleaner code too. IMO.
Hey dude, that’s perfect, much better and slightly more efficient. Thanks for your time once again