Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-14-2011, 10:00 PM   PM User | #1
VickP07
New Coder

 
Join Date: Sep 2011
Posts: 78
Thanks: 15
Thanked 0 Times in 0 Posts
VickP07 is an unknown quantity at this point
select statement help

hey guys okay so here is what i am doing:
i have a page that lists a table of patients that are in the DB if the user clicks on a patients Medical number, which is in the table, then another page is brought up where the user can update prescriptions for this patient.

BUT my problem is this: if a patient has not been assigned a prescription yet the patient is still being displayed since my sql statement says to get all patients in the DB and list them.

I want to do some test if a patient has not been assigned a prescription then dont show them in this table, only show the ones that have.

here is my code that i am using to select all the patients in the DB and put them in the TABLE (in my prescriptions table the table i have a prescription_id (primary) start_date, instructions, dose, pat_id, uom_id, and med_id) so the foreign key pat_id would get the patients id whenever a prescription is added for a certain patient!!!!)

Code:
<?php
include("config.php");
include("lock.php");

if($role != 1) //test to make sure no other users besides doctors can access this page
{
header("Location: Login.php");
}
	

	$sql = "
	SELECT patients.med_rec, patients.fname, patients.lname
	FROM patients;
	"; 
	
	#test if select statement works
	$result = mysql_query( $sql );
	$result or die("My query ($sql) failed." );		
	

?>
VickP07 is offline   Reply With Quote
Old 12-14-2011, 10:33 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Two ways.
Code:
SELECT med_rec, fname, lname
FROM patients 
WHERE id IN ( SELECT pat_id FROM prescriptions )
Or ...
Code:
SELECT DISTINCT patients.med_rec, patients.fname, patients.lname
FROM patients INNER JOIN prescriptions ON patients.id = prescriptions.pat_id
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
VickP07 (12-14-2011)
Old 12-14-2011, 10:34 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
NOTE: You have no ORDER BY clause in your SQL, so the order in which the patients are listed is completely arbitrary. Generally, you should never omit the ORDER BY clause, even when the results look right. There is no guarantee that removing some records and adding some won't mess up the ordering if you omit the ORDER BY.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-14-2011, 10:43 PM   PM User | #4
VickP07
New Coder

 
Join Date: Sep 2011
Posts: 78
Thanks: 15
Thanked 0 Times in 0 Posts
VickP07 is an unknown quantity at this point
thanks works great! quick question even though it really isn't related to this post at all:

Just wanted to know if this is at all possible. I have php page that create tables through html that show data from my database. I want to know if i added like a print button on the page then get whatever is in the table and sent it to a pdf file to print out for a user. are there any examples that u may know of or is this in anyway remotely possible
VickP07 is offline   Reply With Quote
Old 12-15-2011, 12:08 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ummm...if you print from PHP, the printer would have to be co-located with your server. So if the server is in Arizona and the user is in Ohio, that might not be an ideal solution.

If you print from the HTML page, then the printer must be co-located with the user's machine.

So which makes more sense in your situation?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-15-2011, 12:09 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oh...but you *could* use PHP to create a PDF file that the user would then download and print. Is that what you meant?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:38 AM.


Advertisement
Log in to turn off these ads.