Jump to content

Query not returning all rows.


Azyrus

Recommended Posts

 Hello, my code below seems to work great when you enter a value into the input however it does not return all values when the input is empty. It shows rows 1-5 and 79-95 does anyone know how this is happening?

Thank you !

<div id="main">
<form method="post" action="">
<div id="search_query">
Keyword: <input type="text" name="brand">
<input type="submit" name="submit" value="submit">
</div>
</form>
<div id="main_container">
<?php
$db_con = mysql_connect('localhost', 'hwsuser', 'password');
if (!$db_con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('hws', $db_con);
if(!isset($_POST['submit']))
{
$sql = sprintf("SELECT * FROM hws");
$result = mysql_query($sql);
echo "<table>";
echo "<tr>";
echo "<th scope='col'>ID</th>";
echo "<th scope='col'>Brand</th>";
echo "<th scope='col'>Model</th>";
echo "<th scope='col'>Capacity</th>";
echo "<th scope='col'>Loading</th>";
echo "<th scope='col'>Elements</th>";
echo "<th scope='col'>Type</th>";
echo "<th scope='col'>Warranty</th>";
echo "<th scope='col'>Reece</th>";
echo "<th scope='col'>Coop</th>";
echo "<th scope='col'>Samios</th>";
echo "<th scope='col'>KR</th>";
echo "<th scope='col'>BBK</th>";
echo "<th scope='col'>Supply</th>";
echo "<th scope='col'>Continuous</th>";
echo "<th scope='col'>Offpeak</th>";
echo "</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[ID]</td>";
echo "<td>$row[Brand]</td>";
echo "<td>$row[Model]</td>";
echo "<td>$row[Capacity]</td>";
echo "<td>$row[Loading]</td>";
echo "<td>$row[Elements]</td>";
echo "<td>$row[Type]</td>";
echo "<td>$row[Warranty]</td>";
echo "<td>$row[Reece]</td>";
echo "<td>$row[Coop]</td>";
echo "<td>$row[Samios]</td>";
echo "<td>$row[KR]</td>";
echo "<td>$row[BBK]</td>";
echo "<td>$row[Supply]</td>";
echo "<td>$row[Continuous]</td>";
echo "<td>$row[Offpeak]</td>";
echo "</tr>";
}
   echo "</table>";
} 
else

{
$brand = mysql_real_escape_string($_POST['brand']);

$sql = sprintf("SELECT * FROM units WHERE Brand LIKE'$brand' OR Model LIKE'$brand' OR Capacity LIKE'$brand' OR Type LIKE'$brand'");

$result = mysql_query($sql);
	
echo "<table class='altrowstable' id='alternatecolor'>";
echo "<tr>";
echo "<th scope='col'>ID</th>";
echo "<th scope='col'>Brand</th>";
echo "<th scope='col'>Model</th>";
echo "<th scope='col'>Capacity</th>";
echo "<th scope='col'>Loading</th>";
echo "<th scope='col'>Elements</th>";
echo "<th scope='col'>Type</th>";
echo "<th scope='col'>Warranty</th>";
echo "<th scope='col'>Reece</th>";
echo "<th scope='col'>Coop</th>";
echo "<th scope='col'>Samios</th>";
echo "<th scope='col'>KR</th>";
echo "<th scope='col'>BBK</th>";
echo "<th scope='col'>Supply</th>";
echo "<th scope='col'>Continuous</th>";
echo "<th scope='col'>Offpeak</th>";
echo "</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[ID]</td>";
echo "<td>$row[Brand]</td>";
echo "<td>$row[Model]</td>";
echo "<td>$row[Capacity]</td>";
echo "<td>$row[Loading]</td>";
echo "<td>$row[Elements]</td>";
echo "<td>$row[Type]</td>";
echo "<td>$row[Warranty]</td>";
echo "<td>$row[Reece]</td>";
echo "<td>$row[Coop]</td>";
echo "<td>$row[Samios]</td>";
echo "<td>$row[KR]</td>";
echo "<td>$row[BBK]</td>";
echo "<td>$row[Supply]</td>";
echo "<td>$row[Continuous]</td>";
echo "<td>$row[Offpeak]</td>";
echo "</tr>";
}
   echo "</table>";
}
Link to comment
Share on other sites

It might couse of your condition.

$sql = sprintf("SELECT * FROM units WHERE Brand LIKE'$brand' OR Model LIKE'$brand' OR Capacity LIKE'$brand' OR Type LIKE'$brand'");

try it into "=="

$sql = sprintf("SELECT * FROM units WHERE Brand = '$brand' OR Model = '$model' OR Capacity = '$capacity' OR Type = '$type'");
Link to comment
Share on other sites

Are the records that appear the ones that have an empty string value in one or more of those four columns?

 

Use the wildcard character "%" with LIKE. That way if $brand is empty then all values are matched.

$sql = "SELECT * FROM units WHERE Brand LIKE '%$brand%' OR Model LIKE '%$brand%' OR Capacity LIKE '%$brand%' OR Type LIKE '%$brand%'";
Link to comment
Share on other sites

The 'like' query can have the possibility of returning more matches, especially if the argument it is given has the proper wildcards in it. '%PHP%', for instance, will match 'PHPFREAKS' and 'PHP', but as a literal it will not match anything that doesn't have the percent signs in those specific spots. In else part you may have records in DB with exact match.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.