0

Hey guys,
I'm a php newbie that's trying to learn php on the job, and my deadline is quickly approaching. I'm attempting to learn php through a book, which is working just fine. The only problem is it's somewhat of a slow process.

Anyway, I'm trying to search using a physicians name from a database of names and return only the values that are relevant to that search. Results displayed will be all fields for that record.

The table is organized as follows:
Physician - Specialty - Street - City - State - Zip - Phone

I've managed to have it display the entire database (which is 883 doctors long), but I'm having difficulty making it display only relevant information. I'm not getting any errors, but I can't seem to figure out what I'm doing wrong. I'm only trying to display physicians and city to see if I can make it work. Once I get the right code in I was going to expand it to include all 7 fields.

Also, if somebody wants to say I'm posting too much code, then I'll shrink it down to more relevant information. I don't want to annoy anybody here lol. Also, I've double checked my database connections, they are all working properly.

If anybody can assist me or point me into the right direction on how I can narrow my search results to display only relevant information I would greatly appreciate it. Thanks in advance.

<?php 
session_start(); 
// Check for login
// Call DB Connection
include('includes/db_conn.php');
// Search Form Processor
$Physician 	= $_POST['physician'];
$sql = "";
// SQL SELECT 
if(isset($_POST['searchPhysician'])){
	$sql = "SELECT * FROM doctors WHERE Physician LIKE '{$physician}%'";
}//end if
if(isset($_GET['query']) <> ""){
	$query = $_GET['query'];
	$sql = "SELECT Physician, Specialty, Street, City, State, Zip, Phone FROM doctors WHERE Physician = '{$query}'";
	$result = mysql_query($sql);
}//end if
?>
<body>
<h1 class="redLL">Major Topic Search</h1><br>
	<!-- TITLE SEARCH -->
    <form name="searchphysician" method="post" action="<?php $PHP_SELF; ?>">
	<table width="100%" border="0" cellspacing="0" cellpadding="1">
    	<tr>
			<td width="23%" class="description"><p>Search by Name</td>
			<td width="63%"><input name="physician" type="text" id="physician" size="20"></td>
            <td width="14%"><input type="submit" name="searchPhysician" id="search" value="Search"></td>
		</tr>
	</table>
	</form><br  />
    <table border="0" class="table" width="85%" cellspacing="2" cellpadding="2">
    <tr>
        <td class="description" width="20%">
        Physician
        </td>
        <td class="description">
        City
        </td>
    </tr>
<?php 
if($sql !== ""){		
		$result = mysql_query($sql);
		if(mysql_num_rows($result) > 0){
			while($row = mysql_fetch_assoc($result)){
				print "<tr>
						<td>".$row['Physician']."</td>
						<td>".$row['City']."</td>
					  </tr>";
			}//end wh	
		}else{
			print "<tr><td class=\"description\">There are no results to display</td><td>&nbsp;</td></tr>";
		}//end if
	
}//end if
?>
</table>
</div>
<br><br>
                       
</body>
</html>
2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by stoni23
0

Replace line no 11 with the following

$sql = "SELECT * FROM doctors WHERE Physician LIKE '".$Physician."%'";

The problem is you have defined Physician veriable with capital "P" and using lower case "p" in the query. Php veriables are case sensitive.

0

This is true, thank you for noticing that. I have fixed that, but unfortunately I am still having the same problem.


I did forget to mention what is happening when I run the search, it is displaying the entire database whether I input a value from the database or not. It does only include the physicians and their respective cities, but it is displaying all 900 of them.

0

Replace line no 11 with the following

$sql = "SELECT * FROM doctors WHERE Physician LIKE '".$Physician."%'";

The problem is you have defined Physician veriable with capital "P" and using lower case "p" in the query. Php veriables are case sensitive.

The results depend on what you input in the "physicians" text field. If you input nothing it will fetch all the records. If you input "a" It will fetch all the records starting with "a". If you input "ab" It will fetch all results starting with "ab". You can print the sql statement just before eecuting it to find out whether the resultent string is same as you desired. For this add echo $sql; just before executing query at line number 42.

Edited by dasatti: n/a

0

turns out that I forgot to change the physician to Physician in my search form. Thanks a ton for all your help! : )

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.