I have this code which i am testing from last couple of hour but unable to understand that why TodayAttendanceCount is not returning 0.
$sqlCheckLoginEntryCount = "SELECT Count(*) TodayAttendanceCount FROM AttendanceMachineLogin
WHERE Date(RecordAddDateTime) = :RecordAddDateTime
AND TimeTrackId = :TimeTrackId";
$statementEntryCount = $connPDO->prepare($sqlCheckLoginEntryCount);
$queryParams = array(
':TimeTrackId' => $TimeTrackId,
':RecordAddDateTime' => $RecordAddDateTime
);
$statementEntryCount->execute($queryParams);
$queryData = $statementEntryCount->fetch();
echo '\n ';
//var_dump($queryData);
echo "\n Attendance Count". $queryData['TodayAttendanceCount'] ." ;";
I have executed the same query in MySqlWorkbench which is working fine and there is data and it is fine from the database side.
Remove : part at the $queryParams. It isn't needed. The code will look like this.
$queryParams = array(
'TimeTrackId' => $TimeTrackId,
'RecordAddDateTime' => $RecordAddDateTime
);
Try this:
$sqlCheckLoginEntryCount = "SELECT Count(*) As TodayAttendanceCount FROM AttendanceMachineLogin
WHERE Date(RecordAddDateTime) = ':RecordAddDateTime'
AND TimeTrackId = :TimeTrackId";
$statementEntryCount = $connPDO->prepare($sqlCheckLoginEntryCount);
$queryParams = array(
'TimeTrackId' => $TimeTrackId,
'RecordAddDateTime' => $RecordAddDateTime
);
$statementEntryCount->execute($queryParams);
$queryData = $statementEntryCount->fetch(PDO::FETCH_ASSOC);
echo '\n ';
//var_dump($queryData);
echo "\n Attendance Count ". $queryData['TodayAttendanceCount'];
Hope I pushed you further.
Related
I've given a very ugly piece of code which combines PHP loops and SQL queries to retrieve some IDs and I have to refractor. My plan is to merge the PHP iteration to the queries, but I was wondering if a. doing this affect the result and b. it really has good impact on the performance.
The following is a gesture of the current state of the script:
$additional_conditions = [
"test_column1 = 'b1' OR test_column1 = 'b2' ",
"test_column2 = 'c1' OR test_column2 = 'c2' ",
"test_column3 = 'd1' OR test_column3 = 'd2' ",
"test_column4 = 'e1' OR test_column4 = 'e2' ",
"test_column5 = 'f1' OR test_column5 = 'f2' ",
];
$query = 'SELECT * from test_table WHERE test_column = 1 AND %s';
foreach ($additional_conditions as $additional_condition)
{
$result = mysqlQuery(sprintf($query, $additional_condition));
while ($data = mysqli_fetch_assoc($result)) {
// do something with the data
}
}
which I'm thinking to change it to:
$query = <<<QUERY
SELECT * from test_table WHERE test_column = 1 AND (
(test_column1 = 'b1' OR test_column1 = 'b2') OR
(test_column2 = 'c1' OR test_column2 = 'c2') OR
(test_column3 = 'd1' OR test_column3 = 'd2') OR
(test_column4 = 'e1' OR test_column4 = 'e2') OR
(test_column5 = 'f1' OR test_column5 = 'f2')
)
QUERY;
$result = mysqlQuery($query);
while ($data = mysqli_fetch_assoc($result)) {
// do something with the data
}
Your database engine will optimize the query to make it more efficient. It is usually better to perform a heavy database query than a number of dummy queries.
I'm using JQuery FullCalendar version 1.5.4 and I want to set the events with JQuery.
I use $.getJSON to call a php file, that retreive my data in my MySQL database.
When I limit my query with limit 0,1 , everything works fine. The event is sucessfully displayed in the calendar. However, when my php return an array of object, I get the error : Uncaught TypeError: Cannot call method 'replace' of null
the JSON data returned by my PHP Page looks like this :
[{"id":1,"title":"Halloween ","start":"2013-02-08 00:00:00","end":"2013-02-08 00:00:00","allDay":true},{"id":2,"title":null,"start":"2013-02-12 00:00:00","end":"2013-02-12 00:00:00","allDay":true},{"id":3,"title":"Sortie Valcartier","start":"2013-02-20 00:00:00","end":"2013-02-20 00:00:00","allDay":true}]
The PHP code looks like this :
<?php
//header('content-type: application/json; charset=utf-8');
mysql_connect("localhost","root") or die (" NOPE . [" . mysql_error() . "]");
mysql_select_db("garderie");
$query = "select a.ActiviteID as ActiviteID,a.nom as Nom , cd.datedujour as Jour from activites a
inner join calendrieractivite ca
on a.activiteid = ca.activiteid
inner join calendrierdate cd
on ca.dateid = cd.dateid
";
$result = mysql_query($query);
$events = array();
$i = 0;
$year = date('Y');
$month = date('m');
while($ligne = mysql_fetch_array($result))
{
$id = $ligne["ActiviteID"];
$nom = $ligne["Nom"];
$e = array(
'id' => (int)$id,
'title' => $nom,
'start' => $ligne["Jour"],//$start,
'end' => $ligne["Jour"],
'allDay' => true
);
$events[$i] = $e;
$i++;
}
mysql_close();
echo (json_encode($events));
?>
the jquery code looks like this
...
$.getJSON('json_events.php', null, function(data){
$('#calendar').fullCalendar({
events: data,
...
You'll need to filter your events on the php side, and make sure every event has a title. When you pass a null title to FullCalendar, it seems to break the script.
{"id":2,"title":null,"start":"2013-02-12 00:00:00","end":"2013-02-12 00:00:00","allDay":true}
This is sort of a follow up to my last question. Im trying to access the data of a multidimensional array so as to insert into a database. Ive been looking all over the forums as well as trying different things on my own but cant find anything that works. Here is what the array looks like:
$_POST[] = array[stake](
'stakeholder1' => array(
'partner'=> 'partner',
'meet'=> 'meet'
),
'stakeholder2' => array(
'partner'=> 'partner',
'agreement'=> 'agreement',
'train'=> 'train',
'meet'=> 'meet'
),
);
I'm trying to do somthing like ( UPDATE list WHERE stakeholder = "stakeholder1" SET partner =1, meet =1 ) depending on which stakeholder/choices were selected (theres four different options). Thanks,
This one will set 1 for checked options, and 0 for unchecked options (otherwise you will miss some data updates).
$optionsList = array('partner', 'agreement', 'train', 'meet');
$stakeHolders = array('stakeholder1', 'stakeholder2', ...);
foreach($stakeHolders as $stakeHolder)
{
$selectedOptions = $_POST[$stakeHolder];
$arInsert = array();
foreach($optionsList as $option)
$arInsert[] = '`'.$option.'` = '.intval(isset($selectedOptions[$option]));
$sql = "UPDATE list
SET ".implode(", ", $arInsert)."
WHERE stakeholder = '".mysql_real_escape_string($stakeHolder)."'";
// TODO: execute $sql (mysql_query(), or PDO call,
// or any wrapper you use for DB)
}
$query = '';
foreach ($_POST as $k => $v) {
$query .= "UPDATE list WHERE stakeholder = '$k' SET " . implode(" = 1," $v) . ",";
}
$query = substr($query, 0, -2); //Get rid of the final comma
This should give you something like what you are looking for, if I understand your database schema correctly.
If you are using PDO, you could do something like:
foreach($_POST as $stakeholder => $data) {
$sth = $dbh->prepare('UPDATE `list` SET `' . implode('` = 1, `', array_keys($data)) . '` = 1 WHERE stakeholder = ?');
$sth->execute(array($stakeholder));
}
Be sure to sanitize the user input (keys of the data...).
foreach($main_array as $key => $sub_array) {
$sql = 'UPDATE list
WHERE stakeholder = "{$key}"
SET ';
$sets = array();
foreach($sub_array as $column_value)
$sets[] = $column_value." = 1";
$sql = $sql.implode(',', $sets);
}
When I run the following MySQL query via PHP and all of the elements of $_GET() are empty strings, all the records in the volunteers table are returned (for obvious reasons).
$first = $_GET['FirstName'];
$last = $_GET['LastName'];
$middle = $_GET['MI'];
$query = "SELECT * FROM volunteers WHERE 0=0";
if ($first){
$query .= " AND first like '$first%'";
}
if ($middle){
$query .= " AND mi like '$middle%'";
}
if ($last){
$query .= " AND last like '$last%'";
}
$result = mysql_query($query);
What is the most elegant way of allowing empty parameters to be sent to this script with the result being that an empty $result is returned?
my solution:
$input = Array(
'FirstName' => 'first',
'LastName' => 'last',
'MI' => 'mi'
);
$where = Array();
foreach($input as $key => $column) {
$value = trim(mysql_escape_string($_GET[$key]));
if($value) $where[] = "`$column` like '$value%'";
}
if(count($where)) {
$query = "SELECT * FROM volunteers WHERE ".join(" AND ", $where);
$result = mysql_query($query);
}
There's no point in running a (potentially) expensive query if there's nothing for that query to do. So instead of trying to come up with an alternate query to prevent no-terms being searched, just don't run the search at all if there's no terms:
$where = '';
... add clauses ...
if ($where !== '') {
$sql = "SELECT ... WHERE $where";
... do query ...
} else {
die("You didn't enter any search terms");
}
With your current code, if everything is empty, you will get the WHERE 0=0 SQL which is TRUE for all rows in the table.
All you have to do is remove the if statements...
i have this file C:\\xampp\htdocs\exact\sample_pie.php that contains a graph. here's the code:
<?php
include("phpgraphlib.php");
include("phpgraphlib_pie.php");
include("connection.php");
$graph=new PHPGraphLibPie(400,200);
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('exact');
$querypa = "SELECT COUNT(nature) FROM approved WHERE nature='Parade'";
$resultpa = mysql_query($querypa);
$printpa = mysql_result($resultpa,0);
$querycs = "SELECT COUNT(nature) FROM approved WHERE nature='Community Service'";
$resultcs = mysql_query($querycs);
$printcs = mysql_result($resultcs,0);
$queryga = "SELECT COUNT(nature) FROM approved WHERE nature='General Assembly'";
$resultga = mysql_query($queryga);
$printga = mysql_result($resultga,0);
$querypl = "SELECT COUNT(nature) FROM approved WHERE nature='Play/Showcase/Socio-Cultural Show/Film Showing'";
$resultpl = mysql_query($querypl);
$printpl = mysql_result($resultpl,0);
$queryco = "SELECT COUNT(nature) FROM approved WHERE nature='Competition/Sportsfest'";
$resultco = mysql_query($queryco);
$printco = mysql_result($resultco,0);
$queryfr = "SELECT COUNT(nature) FROM approved WHERE nature='Fund Raising'";
$resultfr = mysql_query($queryfr);
$printfr = mysql_result($resultfr,0);
$queryse = "SELECT COUNT(nature) FROM approved WHERE nature='Seminar/Convention/Conference/Training'";
$resultse = mysql_query($queryse);
$printse = mysql_result($resultse,0);
$totalAct=$printpa+$printga+$printpl+$printcs+$printco+$printfr+$printse;
$avepa=($printpa/$totalAct)*100;
$avecs=($printcs/$totalAct)*100;
$avega=($printga/$totalAct)*100;
$avepl=($printpl/$totalAct)*100;
$aveco=($printco/$totalAct)*100;
$avefr=($printfr/$totalAct)*100;
$avese=($printse/$totalAct)*100;
$pa=number_format($avepa,2);
$cs=number_format($avecs,2);
$ga=number_format($avega,2);
$pl=number_format($avepl,2);
$co=number_format($aveco,2);
$fr=number_format($avefr,2);
$se=number_format($avese,2);
$data=array("Parade"=>$pa, "General Assembly"=>$ga, "Play/Showcase/Socio-Cultural Show/Film Showing"=>$pl, "Community Service"=>$cs, "Competition/Sportsfest"=>$co, "Fund Raising"=>$fr, "Seminar/Convention/Conference/Training"=>$se);
$graph->addData($data);
$graph->setTitle("Total Activities per Nature of Activity");
$graph->setPrecision(2);
$graph->setLabelTextColor("0,0,0");
$graph->setLegendTextColor("0,0,0");
$graph->setGradient("210,245,255","pastel_purple");
$graph->createGraph();
?>
how can i get the graph as a .jpg through php script? i tried this file_get_contents("C:\\xampp\htdocs\exact\sample_pie.php");
but it only outputs this:
$pa, "General Assembly"=>$ga, "Play/Showcase/Socio-Cultural Show/Film Showing"=>$pl, "Community Service"=>$cs, "Competition/Sportsfest"=>$co, "Fund Raising"=>$fr, "Seminar/Convention/Conference/Training"=>$se); $graph->addData($data); $graph->setTitle("Total Activities per Nature of Activity"); $graph->setPrecision(2); $graph->setLabelTextColor("0,0,0"); $graph->setLegendTextColor("0,0,0"); $graph->setGradient("210,245,255","pastel_purple"); $graph->createGraph(); ?>
please help me. thanks.
Using file_get_contents() like that is only going to retrieve the script itself. That text is what the file contains and that's what file_get_contents() does; exactly what it says on the tin.
Try looking at the documentation for PHPGraphLib or in the source code failing that. There should be some sort of save method you can call instead of createGraph() or maybe you can just pass a filename parameter to createGraph().
On a sidenote, I should mention that your script is a lot more complicated than it should be. Everything between mysql_select_db() and $graph->addData() could be rewritten into something like this:
$data = array(
'Parade' => 0,
'Community Service' => 0,
'General Assembly' => 0,
'Play/Showcase/Socio-Cultural Show/Film Showing' => 0,
'Competition/Sportsfest' => 0,
'Fund Raising' => 0,
'Seminar/Convention/Conference/Training' => 0
);
$sql = "SELECT nature, COUNT(*) AS cnt
FROM approved
WHERE nature IN ('" . implode("','", array_keys($data)) . "')
GROUP BY nature";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$data[$row['nature']] = $row['cnt'];
}
$totalAct = array_sum($data);
foreach ($data as $nature => &$value)
{
$value = number_format(100 * $value / $totalAct, 2);
}
For the rest, consult the documentation of your graph library as Ollie Saunders suggested.
<img src="/exact/sample_pie.php" />
You may want to use header() to set the appropriate Content-type from within sample_pie.php.