determining percentages with php - php

I am trying to determine the percentage of number of student passing a test either on 1st attempt or 2nd attempt. I believe I have the code correct but when I echo the results I get no output.
<?php
$NREMT1 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive = '1' ";
$NREMT2 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive >= '2' ";
$completed = "SELECT studentstatus FROM course_students WHERE studentstatus >='4'";
$getNREMT1 = mysql_query($NREMT1);
$fetchNREMT1= mysql_num_rows($getNREMT1);
$getNREMT2 = mysql_query($NREMT2);
$fetchNREMT2= mysql_num_rows($getNREMT2);
$getcompleted = mysql_query($completed);
$fetchcompleted= mysql_num_rows($getcompleted);
function percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted) {
$NREMT1count= $fetchNREMT1/$fetchcompleted;
$NREMT1percent= $NREMT1count * 100;
$NREMT1result = number_format($NREMT1percent,0);
$NREMT2count= $fetchNREMT2/$fetchcompleted;
$NREMT2percent= $NREMT2count * 100;
$NREMT2result = number_format($NREMT2percent,0);
}
echo $NREMT1result;
echo $NREMT2result;
?>

You are not calling your function percent anywhere.
You should call it and make it return something or otherwise, just can avoid creating it and put the same code in the same place:
$NREMT1count= $fetchNREMT1/$fetchcompleted;
$NREMT1percent= $NREMT1count * 100;
$NREMT1result = number_format($NREMT1percent,0);
$NREMT2count= $fetchNREMT2/$fetchcompleted;
$NREMT2percent= $NREMT2count * 100;
$NREMT2result = number_format($NREMT2percent,0);
echo $NREMT1result;
echo $NREMT2result;
It seems you don't have it very clear the concept of function. You can take a look at the documentation.

Your problem is that the variables declared inside percent() are never initialized because the function is never called. Try this:
<?php
$NREMT1 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive = '1' ";
$NREMT2 = "SELECT nremtcognitive FROM course_students WHERE nremtcognitive >= '2' ";
$completed = "SELECT studentstatus FROM course_students WHERE studentstatus >='4'";
$getNREMT1 = mysql_query($NREMT1);
$fetchNREMT1= mysql_num_rows($getNREMT1);
$getNREMT2 = mysql_query($NREMT2);
$fetchNREMT2= mysql_num_rows($getNREMT2);
$getcompleted = mysql_query($completed);
$fetchcompleted= mysql_num_rows($getcompleted);
percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted);
function percent($fetchNREMT1, $fetchNREMT2, $fetchcompleted) {
$NREMT1count= $fetchNREMT1/$fetchcompleted;
$NREMT1percent= $NREMT1count * 100;
$NREMT1result = number_format($NREMT1percent,0);
$NREMT2count= $fetchNREMT2/$fetchcompleted;
$NREMT2percent= $NREMT2count * 100;
$NREMT2result = number_format($NREMT2percent,0);
echo $NREMT1result;
echo $NREMT2result;
}
?>
It is also worth noting that the mysql_* family of functions are now deprecated, and you should really consider using MySQLi or PDO.

Just change your query. Untested, but this should get you started:
SELECT
SUM(IF(CONVERT(nremtcognitive, UNSIGNED INTEGER) >= 2)) / COUNT(*)
FROM course_students;

Related

cannot select a row in mysql

EDIT1 : used double quotes and single quotes but I am getting same error.
EDIT2 : same query is returning me result in mysql shell
I am selecting a row from a table.
if(!isset($_GET['title']) || !isset($_GET['user'])){
echo "hi"; //something come here
}
else{
$title = $_GET['title'];
$title = mysqli_real_escape_string($conn,$title);
$user = $_GET['user'];
$user = mysqli_real_escape_string($conn,$user);
echo $title ;
echo $user ;
// tried giving value directly to test but no luck
$query = "SELECT * FROM site WHERE client=\"Chaitanya\" && title=\"werdfghb\" ";
$result5 = mysqli_query($conn,$query) or die(mysqli_error());
$count = mysqli_num_rows($result5);
echo $count ;
while($result9 = mysqli_fetch_array($result5)){
$kk=$result9['url'];
echo $kk ;
}
$page = $kk;
include ( 'counter.php');
addinfo($page);
}
In my database there is a row with columns title and client and the values I entered are in that row but when I echo count(no of rows) it is showing zero.
Is there anything wrong with code ?
The error you are getting is due to the line
$page = $kk;
in this code $kk is not declared previously. The defined $kk is in the while loop scope.
declare the variable like this in the outer scope from the while loop
...
$kk = null;
while($result9 = mysqli_fetch_array($result5)) {
$kk = $result9['url'];
echo $kk ;
}
$page = $kk;
...
Error on Fetching Data
You have to crack you SQl into smaller pieces and test the code like this.
run the query SELECT * FROM site without any where and get the count
run the query SELECT * FROM site WHERE client='Chaitanya' and get the count
SELECT * FROM site WHERE title='werdfghb' and check the count
Then run the whole query
And see the results. This way u can find out in where the issue is in your SQL code. I prefer you use the mysql client to execute this queries
As I pointed out in my comment, $kk is undefined in the $page = $kk;, since it is declared in the while loop.
Do something like:
$kk = ''; //can also do $kk=NULL; if you want.
while($result9 = mysqli_fetch_array($result5)) {
$kk=$result9['url'];
echo $kk ;
}
$page = $kk;
try this one
$client = "Chaitanya";
$title = "werdfghb";
$query="SELECT * FROM site WHERE client='".$client."' and title='".$title."' ";
you can also use this
$query="SELECT * FROM site WHERE client={$client} and title={$title} ";

Generating a sql query in php using for loop

CODE :
$nerd_result = mysql_query("select * from nerd_profile where nerd_reg_no = '$reg_no'");
$nerd_data = mysql_fetch_array($nerd_result);
$tags = array();
$tags = explode(",",$nerd_data['nerd_interests']);
for($i = 0; $i < sizeof($tags)-1; $i++)
{
if($i != sizeof($tags)-2)
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% or ";
}
else
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% ";
}
}
$proper_query = "select * from `qas_posts` where ".$sub_query." and `post_date` like '%$today%'";
$result = mysql_query($proper_query);
while($each_qas = mysql_fetch_array($result))
Description :
I am adding the like clause along with php variable in a string and concatenating it with the further variables with like clause to come. In the end when I echo I get the perfect query that I want but
mysql_fetch_array()
does not accept that generated query rather if I hard code it , it works perfect what am I doing wrong ?? can I do that ??
When doing string comparisons in mysql you need to make sure you have quotes around your comparison value.
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' or ";
and
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' ";

selecting in php WHERE x = $array[index]?

I've tried searching for answers to my problem but no one else seems to have had this problem! I'm basically trying to select in php using the WHERE statement, I want to compare my $ID to variables stores in an array called $resultAddID2. This array has values [1,2,1]; if I try accessing $resultAddID2[0] it works fine, but if I try to do it with $resultAddID2[1] or $resultAddID2[2] it doesn't work at all! I'm sure this is something silly but for the life of me I just can NOT figure it out! Any help would be much appreciated, thank you.
Here's the part where I try to do this:
$resultAddID = mysql_query("SELECT ADDRESS_ID FROM hospital");
while($resultAddID1=mysql_fetch_array($resultAddID)){
$resultAddID2[]=$resultAddID1['ADDRESS_ID'];
for ($i = 0; $i <= 2; $i++) {
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = $resultAddID2[]");
$resultAddTm= mysql_fetch_array($resultAddT);
$resultAddT2[]=$resultAddTm['GOVER_ID'];
}
}
$response["hospADD"]= $resultAddT2;
You should add {}'s like
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = {$resultAddID2[$i]}");
It would be better if you did something like this because you are querying the second item too many times
$resultAddID = mysql_query("SELECT ADDRESS_ID FROM hospital");
while($resultAddID1=mysql_fetch_array($resultAddID)){
$resultAddID2[]=$resultAddID1['ADDRESS_ID'];
}
for ($i = 0; $i <= 2; $i++) {
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = {$resultAddID2[$i]}");
$resultAddTm= mysql_fetch_array($resultAddT);
$resultAddT2[] = $resultAddTm['GOVER_ID'];
}
$response["hospADD"]= $resultAddT2;
OR simpler yet:
$resultAddID = mysql_query("SELECT ADDRESS_ID FROM hospital");
while($resultAddID1=mysql_fetch_array($resultAddID)){
$resultAddT = mysql_query("SELECT * FROM address WHERE ID = {$resultAddID1['ADDRESS_ID']}");
$resultAddTm= mysql_fetch_array($resultAddT);
$resultAddT2[] = $resultAddTm['GOVER_ID'];
}
$response["hospADD"]= $resultAddT;

MySQL sorting with PHP

I'm trying to accomplish the following situation:
$mysql_query = "
SELECT *
FROM st_users
WHERE
`user_comp_supervisor_id` = '$team_supervisor' AND
`user_exempt_from_goals` = '0'
ORDER BY 'calculate_progress_percent()' ASC
";
I know that I can't accomplish ordering by a function in a MySQL statement, but I'm trying to figure out how to take all the returned records, and then order them in order of highest to lowest from a php function result. Any ideas would be greatly appreciated; I've been trying to wrap my head around this for a few hours now... :-(
function diy_calc_progress_percent($user_id,$period_id,$period_week_number)
{
$this->user_id = $user_id;
$this->period_id = $period_id;
$this->period_week_number = $period_week_number;
if ($this->period_week_number == 1)
{
$this->week_id = mysql_result( mysql_query(" SELECT `period_week_one` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
}
else if ($this->period_week_number == 2)
{
$this->week_id = mysql_result( mysql_query(" SELECT `period_week_two` FROM `st_comp_periods` WHERE `period_id` = '$this->period_id' "),0 );
}
else
{
echo "Week number not valid.";
exit();
}
$this->week_start_date = mysql_result( mysql_query(" SELECT `week_start_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );
$this->week_end_date = mysql_result( mysql_query(" SELECT `week_end_date` FROM `st_comp_weeks` WHERE `week_id` = '$this->week_id' "),0 );
$this->user_department = $this->user_info($this->user_id,"user_comp_department_id");
$this->user_week_diy_goal = mysql_result( mysql_query(" SELECT `goal_diy_department` FROM `st_comp_department_goals` WHERE `goal_department_id` = '$this->user_department' AND `goal_week_id` = '$this->week_id' "),0 );
$this->calc_totals_result = mysql_query("SELECT SUM(record_total_diy_revenue) AS user_week_total FROM `st_entered_records` WHERE `record_user_id` = '$this->user_id' AND `record_date` BETWEEN '$this->week_start_date' AND '$this->week_end_date'");
$this->calc_totals_row = mysql_fetch_assoc($this->calc_totals_result);
$this->user_week_total = $this->calc_totals_row['user_week_total'];
$this->user_week_one_percent = ($this->user_week_total / $this->user_week_diy_goal) * 100;
$this->user_week_one_percent = number_format( (float)$this->user_week_one_percent, 2, '.', '' );
return $this->user_week_one_percent;
}
You probably will have to do some array juggling.
First get all your entries FROM st_users into a first array (mysql_query)
Then you could run through that array, and for each entry you do the calculate_progress_percent() and build up a second array in which you could add the additional info ("user_progress_percent").
After this you can sort the new array ba your new info ("user_progress_percent").
And here is some quick and dirty code-suggestions – code is however not tested… of course…:)
First:
$mysql_query = "SELECT * FROM st_users
WHERE `user_comp_supervisor_id`='$team_supervisor' AND
`user_exempt_from_goals` = '0'";
Then something like this:
$i = 0;
while($tmp = mysql_fetch_array($mysql_query)) {
$my_second_array[$i]['user_id'] = $tmp['user_id'];
$user_id = $my_second_array[$i]['user_id'];
diy_calc_progress_percent($user_id,$period_id,$period_week_number);
$my_second_array[$i]['user_result'] = $diy_calc_progress_percent_result;
$i++;
}
And then sorting that second array should be possible as described here:
Sort Multi-dimensional Array by Value
…hope this helps at some point…

Variable in a mysql query

for ($i=0; $i<$count; $i++) {
$appid = $chk[$i];
include "dbconnect.php";
$selectquery = mysql_query("SELECT * FROM regform_admin WHERE tid = '$appid'");
$fetch = mysql_fetch_array($selectquery);
$tid = $fetch['tid']; $username = $fetch['username']; $c_month = $fetch['month']; $c_day =$fetch['day']; $c_year = $fetch['year'];
$c_month2 = $fetch['month2']; $c_day2 =$fetch['day2']; $c_year2 = $fetch['year2'];
$pickup = "".$c_month."/".$c_day."/".$c_year."";
$return = "".$c_month2."/".$c_day2."/".$c_year2."";
$pickuploc = "".$fetch['pickupret']." "." ".$fetch['speclocation']."";
$desti = "".$fetch['destination']." "." ".$fetch['location']."";
$vehicle1 = $fetch['vehicle1'];
$datesent = date("n j, Y; G:i"); ;
$rand = rand(98765432,23456789);
include "vehicledbconnect.php";
$vquery = mysql_query("SELECT * FROM vehicletbl WHERE vehicle = '$vehicle1'");
$getvquery = mysql_fetch_array($vquery);
$maxcars = $getvquery['maxcars'];
$carsleft = $getvquery['carsleft'];
if ($carsleft == 0) {
echo '
<script language="JavaScript">
alert("Cannot move reservation to Pending for payment status. No available vehicles left for this reservation.");
</script>';
echo "$vehicle1";
}
Hi guys my problem here is that the $vehicle is not returning its values if it is inserted in a database query ($vquery = mysql_query("SELECT * FROM vehicletbl WHERE vehicle = '$vehicle1'");) but if it is echoed, it return its value. The logic here is that it will select all the values from vehicletbl wherein the value of any values in 'vehicle' column will be equal to the $vehicle1. Thanks for the help!
You've got ZERO error handling on your queries. Try adding some debugging to the query calls:
$result = mysql_query(...) or die(mysql_error());
The rest of the code is ugly, but looks "ok", so start looking at WHY you're not getting anything back from the queries.
Never ever assume a query succeeds.
try this to debug :
$sql = "SELECT * FROM vehicletbl WHERE vehicle = '" . $vehicle1 . "'";
$vquery = mysql_query($sql) or die(mysql_error() . "\n<br>$sql");
thats what i do to find errors in my sql.
Noob programmer ? Here are some things to know :
for ($i=0; $i<$count; $i++) {
$appid = $chk[$i];
// Replaced By ...
foreach($chk as $appid){
http://php.net/manual/en/control-structures.foreach.php
// Include the file before the loop ! You're including 20 times your file, but you just need to do it once ! Another thing to know:
include_once("dbconnect.php");
http://php.net/manual/en/function.include-once.php
$desti = "".$fetch['destination']." "." ".$fetch['location']."";
// WHY ?? Isn't that easier to do this ?
$desti = $fetch['destination']." ".$fetch['location'];
And security :
// Don't forget to escape your variables before putting it in mysql queries
$appid = mysql_real_escape_string($appid);
$selectquery = mysql_query("SELECT * FROM regform_admin WHERE tid = '$appid'");
Best way to defend against mysql injection and cross site scripting
There are other remarks, but try to improve those points first !

Categories