Forum Moderators: coopster

Message Too Old, No Replies

how to query one column?

         

shams

12:49 pm on Oct 10, 2006 (gmt 0)

10+ Year Member



hi,
this is php mysql query script from the patients table register, i want when i enter one value for example for sex m (male),the script will show only male patients all rows,but this script show all the registered parients in any query, any idea please?

<?php
include 'library/config.php';
include 'library/opendb.php';
$id=$_POST['id'];
$name=$_POST['name'];
$fatherN=$_POST['fatherN'];
$sputum=$_POST['sputum'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$address=$_POST['address'];
$nationality=$_POST['nationality'];
$attCat=$_POST['attCat'];
$disClass=$_POST['disClass'];
$site=$_POST['site'];
$type=$_POST['type'];
$patientEnd=$_POST['patientEnd'];
$query = "SELECT * FROM register WHERE id='$id' or sp0='$sp0' or sex='$sex' or age='$age' or address='$address' or nationality='$nationality' or attCat='$attCat' or diseaseClass='$disClass' or site='$site' or type='$type' or patientEnd='$patientEnd' or name='$name' and fatherN='$fatherN'";

$result = mysql_query($query) or die(mysql_error());

echo "<table border='1'>";
echo "<tr><th>Id</th><th>Date Of Reg</th><th>NAME</th><th>Father/N</th><th>Sex</th><th>Age</th><th>Address</th><th>nationality</th><th>Phone</th>
<th>oldTbId</th><th>oldTbDate</th><th>Date ATT Started</th><th>ATT Catagory</th><th>Disease Class</th><th>Site of disease</th><th>Type</th><th>First Weight</th><th>First Sputum</th><th>Sputum Date</th><th>Lab NO</th><th>First C/S</th><th>Date ATT Stopped</th><th>Patient End</th><th>Remarks</th></tr>";

while($row = mysql_fetch_array($result)) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['dateReg'];
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
echo $row['fatherN'];
echo "</td><td>";
echo $row['sex'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['address'];
echo "</td><td>";
echo $row['nationality'];
echo "</td><td>";
echo $row['phone'];
echo "</td><td>";
echo $row['oldTbId'];
echo "</td><td>";
echo $row['oldTbDate'];echo "</td><td>";
echo $row['dateAttStar'];
echo "</td><td>";
echo $row['attCat'];
echo "</td><td>";
echo $row['diseaseClass'];
echo "</td><td>";
echo $row['site'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>";
echo $row['weight0'];
echo "</td><td>";
echo $row['sp0'];
echo "</td><td>";
echo $row['sp0Date'];
echo "</td><td>";
echo $row['sp0Lab'];
echo "</td><td>";
echo $row['cs1'];
echo "</td><td>";
echo $row['dateStop'];
echo "</td><td>";
echo $row['patientEnd'];
echo "</td><td>";
echo $row['remarks'];
echo "</td></tr>";
}
echo "</table>";
include 'library/closedb.php';
?>

[edited by: dreamcatcher at 5:24 pm (utc) on Oct. 10, 2006]
[edit reason] Fixed side scroll. [/edit]

eelixduppy

4:41 pm on Oct 10, 2006 (gmt 0)



Something like this?:

[b]$query = "SELECT * FROM register WHERE sex = 'male'";[/b]
$result = mysql_query($query) or die(mysql_error());
echo '<pre>';
while($row = mysql_fetch_array($result)) {
print_r($row);
}
echo '</pre>';

The highlight of this example is the query.

MattAU

8:32 pm on Oct 10, 2006 (gmt 0)

10+ Year Member



I'd say the problem is your query is matching on the fields that = ''. For example, if you search for sex = 'm' all patients with site = '', type = '' or sp0 = '' etc. will still turn up in the results.

The way to fix this is to build your query on the fly. I think the easiest way to do this would be an array and a loop

$fields = array('id','name','sex'...);
$query = "SELECT * FROM register";
$first_field = true;
foreach($fields as $field)
{
if(!empty($_POST[$field]))
{
if($first_field)
{
$query .= " WHERE ";
$first_field = false;
}
else
{
$query .= " OR ";
}
$query .= $field . " = '" . $_POST[$field] . "'";
}
}

shams

10:27 am on Oct 11, 2006 (gmt 0)

10+ Year Member



thanks for replies i use the last posted code in my script but the output is just the table headers no data retrived from the table register this is what i use:

<html>
<head><title>query regiser</title>
<style type="text/css">
table tr td { font: 12px Verdana, Arial, sans-serif;
text-align: center;
font-weight: bold;
}
table tr th { font-size: 12px;
text-align: center;
}
</style>
<head>
<body>
<?php
include 'library/config.php';
include 'library/opendb.php';
$id=$_POST['id'];
$name=$_POST['name'];
$fatherN=$_POST['fatherN'];
$sputum=$_POST['sputum'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$address=$_POST['address'];
$nationality=$_POST['nationality'];
$attCat=$_POST['attCat'];
$disClass=$_POST['disClass'];
$site=$_POST['site'];
$type=$_POST['type'];
$patientEnd=$_POST['patientEnd'];

$fields = array('id', 'dateReg', 'name', 'fatherN', 'sex', 'age', 'address', 'nationality', 'phone', 'oldTbId', 'oldTbDate', 'dateAttStar', 'attCat', 'diseaseClass', 'site', 'type', 'weight0', 'sp0', 'sp0Date', 'sp0Lab', 'cs1', 'dateStop', 'patientEnd');
$query = "SELECT * FROM register";
$first_field = true;
foreach($fields as $field)
{
if(!empty($_POST[$field]))
{
if($first_field)
{
$query .= " WHERE ";
$first_field = false;
}
else
{
$query .= " OR ";
}
$query .= $field . " = '" . $_POST[$field] . "'";
}
}
mysql_query($query) or die(mysql_error());

echo "<table border='1'>";
echo "<tr><th>Id</th><th>Date Of Reg</th><th>NAME</th> <th>Father/N</th> <th>Sex</th> <th>Age</th> <th>Address</th> <th>nationality</th> <th>Phone</th> <th>oldTbId</th> <th>oldTbDate</th><th>Date ATT Started</th><th>ATT Catagory</th><th>Disease Class</th><th>Site of disease</th><th>Type</th><th>First Weight</th><th>First Sputum</th><th>Sputum Date</th><th>Lab NO</th><th>First C/S</th><th>Date ATT Stopped</th><th>Patient End</th><th>Remarks</th></tr>";

while($row = mysql_fetch_array($result)) {
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['dateReg'];
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
echo $row['fatherN'];
echo "</td><td>";
echo $row['sex'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['address'];
echo "</td><td>";
echo $row['nationality'];
echo "</td><td>";
echo $row['phone'];
echo "</td><td>";
echo $row['oldTbId'];
echo "</td><td>";
echo $row['oldTbDate'];
echo "</td><td>";
echo $row['dateAttStar'];
echo "</td><td>";
echo $row['attCat'];
echo "</td><td>";
echo $row['diseaseClass'];
echo "</td><td>";
echo $row['site'];
echo "</td><td>";
echo $row['type'];
echo "</td><td>";
echo $row['weight0'];
echo "</td><td>";
echo $row['sp0'];
echo "</td><td>";
echo $row['sp0Date'];
echo "</td><td>";
echo $row['sp0Lab'];
echo "</td><td>";
echo $row['cs1'];
echo "</td><td>";
echo $row['dateStop'];
echo "</td><td>";
echo $row['patientEnd'];
echo "</td><td>";
echo $row['remarks'];
echo "</td></tr>";
}
echo "</table>";
include 'library/closedb.php';
?>
</body>
<html>

[edited by: jatar_k at 3:20 pm (utc) on Oct. 11, 2006]
[edit reason] fixed sidescroll [/edit]