Jump to content


Photo

please check the mistakes of this php code?


  • Please log in to reply
6 replies to this topic

#1 shams

shams
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 14 May 2006 - 03:57 PM

hi,
i have the patients mysql table name is treatment, patients will collect thier drugs each month in the same date, it means for each patiens will be multiple entries with deferent id's because the id is auto_increment, ecach drug has it is own column, so the quantity will be entered in the ecah drugs column, i will enter the drug name and the months for that drug will be taken by the patients, this script will tell me the quanttity of that drug i need for the future:
<?php
$drug = 'PZA'; #$_POST['drug'];
$months = '4'; #$_POST['type'];

mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
$result = mysql_query("SELECT SUM($drug) as total FROM treatment");
$total = mysql_result($result, 0, 'total');
$no = mysql_query("SELECT count($drug) FROM treatment");
$newtot = (($total/$no)*$months) - $total;
if($newtot) {
 echo "You need ".$newtot." " ."tab of " .$drug;
}
else { echo "Sorry no match found";
}

?>
but the output of this script is sorry no match found which is not the correct answer, any one can help please?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 May 2006 - 04:07 PM

Your SQL queries are little wrong i belive. I think you should be doing something like this instead:
$result = mysql_query("SELECT SUM(drug_col) as total FROM treatment WHERE drug_col='$drug'");
Note: before using that query make sure you change any instance of drug_col to the column name you are getting drug name from.

Also your secound query will need to be modified like above too.

#3 crouchjay

crouchjay
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 14 May 2006 - 04:56 PM

mysql_connect("localhost","root") or die(mysql_error());

I am not totally sure, but I do believe you need a password in there.

Jay

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 May 2006 - 05:04 PM

[!--quoteo(post=373776:date=May 14 2006, 05:56 PM:name=crouchjay)--][div class=\'quotetop\']QUOTE(crouchjay @ May 14 2006, 05:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
mysql_connect("localhost","root") or die(mysql_error());

I am not totally sure, but I do believe you need a password in there.

Jay
[/quote]
He most probably took the password out for security reasons or hes developing the script locally on his local PC and his seup of mysql has no password set. So that isn't the problem but mainly to do with his SQL queries.

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 14 May 2006 - 05:34 PM

And the fact that mysql_query() returns a result resource, not an actual result.

Look at [man]mysql_fetch_assoc[/man]().

#6 shams

shams
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 15 May 2006 - 12:33 AM

thanks to all for replies, as i menttioned the drug_col is same as $drug, i means $drug is the column name as well the drug name, each indivdual query work fine, but don't know how to combine them in one php code.

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,015 posts

Posted 15 May 2006 - 07:42 AM

As Thorpe indicated,

change
$no = mysql_query("SELECT count($drug) FROM treatment");


to
$result = mysql_query("SELECT count($drug) FROM treatment");
$no = mysql_result ($result,0,0);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users