I just asked a question earlier, but now i have formatted it into a function that does not return any value.
This is my code:
echo GetHours($UID, $DAY, $MONTH, $YEAR);
function GetHours($UserId, $Day, $Month, $Year){
//filter queries:
//YEAR:
if($Year==FALSE){
$Y = "";
} else {
$Y = " AND Year = '$Year'";
}
//MONTH:
if($Month==FALSE){
$M = "";
} else {
$M = " AND Month = '$Month'";
}
//DAY:
if($Day==FALSE){
$D = "";
} else {
$D = " AND Day = '$Day'";
}
$Query = mysql_query("SELECT SUM(TotalHrs)
FROM WorkLog
WHERE UserId = '$UserId'$D$M$Y");
$Data = mysql_fetch_array($Query);
return $Data;
}
Now, i do know that mysql_ functions are depreciated, but its required for this application at the moment.
My current problem is that this function does not return anything after using GET parameters to test.
Any solutions to this?
EDIT
I have changed the last lines to: return json_encode($Data); and now the screen shows: {"0":"8","SUM(TotalHrs)":"8"}
Always check that mysql_query() has not returned an error! Specially if you are building your query from parts generated from inputs that may or may not be present.
This should at least identify any errors in the SQL.
function GetHours($UserId, $Day, $Month, $Year){
//filter queries:
//YEAR:
if($Year==FALSE){
$Y = "";
} else {
$Y = " AND Year = '$Year'";
}
//MONTH:
if($Month==FALSE){
$M = "";
} else {
$M = " AND Month = '$Month'";
}
//DAY:
if($Day==FALSE){
$D = "";
} else {
$D = " AND Day = '$Day'";
}
$Query = mysql_query("SELECT SUM(TotalHrs)
FROM WorkLog
WHERE UserId = '$UserId'$D$M$Y");
if ( ! $Query ) {
// just debug code, should be amended for a live site situation
echo mysql_error();
return 'Its broken';
}
$Data = mysql_fetch_array($Query);
return $Data;
}
I figured out that the data i get returned was an array by doing echo json_encode(GetHours($UID, $DAY, $MONTH, $YEAR));
My finished code that works:
<?php
include 'assets/db_connect.php';
//Function to get total from WorkLog:
$DAY = $_GET['day'];
$MONTH = $_GET['month'];
$YEAR = $_GET['year'];
$UID = $_GET['id'];
echo GetHours($UID, $DAY, $MONTH, $YEAR);
function GetHours($UserId, $Day, $Month, $Year){
//filter queries:
//YEAR:
if($Year==FALSE){
$Y = "";
}
else{
$Y = " AND Year = '$Year'";
}
//MONTH:
if($Month==FALSE){
$M = "";
}
else{
$M = " AND Month = '$Month'";
}
//DAY:
if($Day==FALSE){
$D = "";
}
else{
$D = " AND Day = '$Day'";
}
$Query = mysql_query("SELECT SUM(TotalHrs) FROM WorkLog WHERE UserId = '$UserId'$D$M$Y");
$Data = mysql_fetch_array($Query);
return $Data['SUM(TotalHrs)'];
}
Related
I know that the error says that format isn't a valid code, but I used it before and it worked fine. So I hope someone knows a code to replace it with or now how to fix it.
require_once 'config.php';
$checker = "SELECT StartTime FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$checker2 = "SELECT EndTime FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$checker3 = "SELECT Minutes FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$result1 = mysqli_query($mysqli, $checker);
$result2 = mysqli_query($mysqli, $checker2);
$minutes = mysqli_query($mysqli, $checker3);
$starttime = new DateTime($result1);
$endtime = new DateTime($result2);
while($starttime <= $endtime)
{
echo "<option value='".$starttime->format('H:i:s')."'>".$starttime->format('H:i:s')."</option>";
$starttime = date_add($starttime, date_interval_create_from_date_string($minutes, ' min'));
}
echo " </select>";
and above I grab the vars out of the database
The problem is mysqli_query returns a mysqli_result which you need to read before using:
require_once 'config.php';
$checker = "SELECT StartTime, EndTime, Minutes FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$result = mysqli_query($mysqli, $checker); //Execute query
if (!$result) { //Result may be false if there's an error
echo "Error getting result";
}
$row = mysqli_fetch_assoc($result); //Put first result in an array
if (!$row) { // $row will be false if there's no result
echo "No data found";
}
//Notice how the array entry keys match the columns in the query.
$starttime = new DateTime($row["StartTime"]);
$endtime = new DateTime($row["EndTime"]);
$minutes = $row["Minutes"];
while($starttime <= $endtime)
{
echo "<option value='".$starttime->format('H:i:s')."'>".$starttime->format('H:i:s')."</option>";
$starttime = date_add($starttime, date_interval_create_from_date_string($minutes, ' min'));
}
echo " </select>";
I'm working with SQLite on my webserver and have had no problems until now.
$sql = "SELECT * from TeammateCurrent;";
$ret = $db->query($sql);
if($ret != false) {
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
$uniqueid = $row['uniqueID'];
$name = $row['Name'];
$lineID = $row['LineID'];
$job = $row['Job'];
$sunday = $row['Sunday'];
$monday = $row['Monday'];
$tuesday = $row['Tuesday'];
$wednesday = $row['Wednesday'];
$thursday = $row['Thursday'];
$friday = $row['Friday'];
$saturday = $row['Saturday'];
//echo $name."<br>".$lineID."<br>".$job."<br>".$sunday."<br>".$monday."<br>".$tuesday."<br>".$wednesday."<br>".$thursday."<br>".$friday."<br>".$saturday."<br><br>";
$sql = "INSERT INTO TeammateHistory (uniqueID, Name,LineID,Job,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)
VALUES (NULL, '$name','$lineID','$job','$sunday','$monday','$tuesday','$wednesday','$thursday','$friday','$saturday');";
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
}
$sql2 = "UPDATE TeammateCurrent set Sunday='$day0', Monday='$day1', Tuesday='$day2', Wednesday='$day3', Thursday='$day4', Friday='$day5', Saturday='$day6' where uniqueID='$uniqueid';";
$ret = $db->exec($sql2);
if(!$ret){
echo $db->lastErrorMsg();
} else {
}
}
} else {
echo "Query not successful";
}
When the code above executes, I get the error Fatal error: Call to a member function fetchArray() on a non-object in /home/xxxx/public_html/xxxx.com/trg/rc2/db/startNewWeek.php on line 39
I've ran code just like this on other pages and they work fine, I'm at a total loss here.
My tables:
you're reusing the variable $ret. You should have a different return variable for the SELECT statment and UPDATEs. In the second loop you're trying to do a fetchArray() of the return of an UPDATE statement.
I need the $data to return from the database if the data was found from the database, but unfortunately, my code is not working to return the data.
I tried to enter non-exist data, it managed to return the error message but if the data exist, the page displays nothing.
May I know where did I go wrong in my if statement?
Below is my code that contain after the $data return the script will execute.
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$dob = implode('/', array($day,$month,$year));
if(isset($_POST['submit'])){
if(empty($_POST['day']) || empty($_POST['month']) || empty($_POST['year'])){
echo "You need to fill in each field.<br /><br />Click here to <b>fill in each field</b> again";
exit;
}
}
//Date that user keyin
$userDate = $dob;
$dateTime = DateTime::createFromFormat('d/m/Y', $userDate);
$myFormat = $dateTime->format('Y-m-d');
//Query string and put it in a variable.
if(isset($_POST['dob_chi'])){
$query = "SELECT * FROM $table_n WHERE dob_chi = :date";
} else {
$query = "SELECT * FROM $table_n WHERE dob_eng = :date";
}
$stmt = $db->prepare($query);
$stmt->execute(array('date' => $myFormat));
$data = $stmt->fetchAll();
if ( !$data ) {
echo 'No data found in database!';
exit;
} else {
return $data=$userDate;
}
//Now we create a while loop for every entry in our DB where the date is match.
while ($row = $stmt->fetchObject()) {
$r1 = $row->rowone;
$r2 = $row->rowtwo;
$r3 = $row->rowthree;
$englishdate = $row->dob_eng;
$chinesedate = $row->dob_chi;
$zodiac = $row->zodiac;
//add all initial data into a matrix variable for easier access to them later
//To access rowone use $rows[0][0], rowtwo $rows[1][0] ect.
//The matrix is an array which contains multiple array. eg. 2-dimensional arrays
//To get all the variables with $r1X simply retrieve the first array of the matrix eg $rows[0]
$rows = array(array($r1),array($r2),array($r3),array());
}
//Similarities between row1 and row2 made me incorporate modulo value as an argument.
function incmod($a, $m){
return ($a % $m) + 1;
}
Thank you for your time
Get rid of the line:
return $data=$userDate;
because it's ending your script and also overwriting the $data variable.
Change:
$data = $stmt->fetchAll(PDO::FETCH_OBJ);
Then change the line:
while ($row = $stmt->fetchObject()) {
to:
foreach ($data as $row) {
I am trying to find out how many days between two date the code bellow is an example of what i am saying but it does not work all what i get in the database is 0
<?php
$hotel_name = $_POST['hotelName'];
$room_type = $_POST['roomType'];
//date From value
$dayfrom = $_POST['dayfrom'];
$monthfrom = $_POST['monthfrom'];
$yearfrom = $_POST['yearfrom'];
//date To value
$dayto = $_POST['dayto'];
$monthto = $_POST['monthto'];
$yearto = $_POST['yearto'];
$arivDate = sprintf('%04d-%02d-%02d', $yearfrom, $monthfrom, $dayfrom);
$depDate = sprintf('%04d-%02d-%02d', $yearto, $monthto, $dayto);
$child = $_POST['child'];
$adult = $_POST['adult'];
$days = $arivDate - $depDate;
//$sortedfromdate = strtotime($arivDate);
//$sortedtodate = strtotime($depDate);
$query = "INSERT INTO tempbooking( book_date, Ariv_date, dep_date, hotel_name
)VALUES(
CURDATE(), '{$arivDate}', '{$depDate}', '{$hotel_name}')";
if(mysql_query($query,$connection)){
$booking_id = mysql_insert_id();
}else{
die("The Booking was not successful". mysql_error());
}
$queryres="INSERT INTO ro_reservation( booking_id, children, adult, room_type, number_days
)VALUES(
{$booking_id}, {$child}, {$adult}, '{$room_type}',{$days})";
if(mysql_query($queryres,$connection)){
header("Location:index.php");
//echo" Reservation Inserted";
}else{
echo "Nothing was inserted in to the ro_reservation";
}
?>
$arivDate = strtotime("2012-01-01");
$depDate = strtotime("2012-08-14");
$datediff = abs($arivDate - $depDate)
echo floor($datediff/(60*60*24));
I try to make a diagram with RGraph(JS). I have a user counter on my site. And now I want to make a user-statistic with this diagram.
However I created a list which should be used for every query (made a form to choose month and year).
My problem is that the code doesnt enter the loop to create the new values (Day and Count).
Any idea?
Here the code:
$days = 0;
$day_value = array();
//check months which have 30 days
if (in_array($month, array(4,6,9,11))) {
$days = 30;
}
//leapyear?
if ($month == 2) {
if ($year%4 == 0) {
$days = 29;
} else {
$days = 28;
}
}
//Count users for each day
for ($i=1; $i<=$days; $i++) {
$sql = "SELECT
Anzahl
FROM
Counter
WHERE
YEAR(Datum) = '".$year."' AND
MONTH(Datum) = '".$month."' AND
DAYOFMONTH(Datum) = '".$i."'";
if (!$result = $db->query($sql)) {
return $db->error;
}
$row = $result->fetch_assoc();
$day_value[$i] = (int)$row['Anzahl'];
}
//delete list for new month/year
$sql = "DELETE
FROM
Statistics
";
if (!$result = $db->query($sql)) {
return $db->error;
}
//Create list with values for each day
for ($j=1; $j<=$days; $j++) {
$sql = "INSERT INTO
Statistics(Day,Count)
VALUES
(?, ?)";
$stmt = $db->prepare($sql);
if (!$stmt) {
return $db->error;
}
$stmt->bind_param('ii', $j, $day_value[$j]);
if (!$stmt->execute()) {
return $stmt->error;
}
$stmt->close();
}
$days is set to 0, it only changes if $months is 2,4,6,9,11 so for every other value of $month $days will be 0 and not enter the for loop.
if (in_array($month, array(4,6,9,11))) {
//code
}
elseif ($month == 2) {
//code
}
else{
//code
}