SQL Error: 'Fatal Error' - php

I am trying to output some minutes 7 days after a meeting has taken place. I have inserted in the following code only to get a fatal error message.
<?php
$id = $_GET['meeting_id'];
$from = date( 'Y-m-d', strtotime( $row['date_update'] . '-7 days' ) );
$result = $this->db->get ('SELECT * FROM Meetings INNER JOIN Minutes ON Minutes.meeting_id = Meetings.meeting_id WHERE Rooms.date >= "' . $from . '" AND Rooms.date <= NOW() AND Minutes.meeting_id = $id')
or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo '<h3>There Arent Any Minutes For This Meeting Yet</h3>';
} else {
while($info = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><br/>" .'Title: '. $info['title']." </td>";
echo "<td><br/><br/>" .'Subject: '. $info['subject']. "</td>";
echo "<td><br/><br/>" .'Next Subject: '. $info['next_subject']."</td>";
echo '<br/><br/>Attendees';
echo ' | Apologies';
}
}
echo "</tr>";
echo "</table>";
?>
the error message i am getting is as follows:
Fatal error: Using $this when not in object context in E:\webareas\hj942\CW\meetings\conference\viewminutes.php on line 59
line 59 is the query at $result.
anyone?

"$this" is used inside a Class to refer to itself. As you're not inside a class, the php script produces an error. Use mysql_query() instead, and of course you'll need to connect to your DB first

You are using $this->db on the 5th line of your code and it's not a object context. You probably want to use $db, but also you need to initiate db connection on $db variable for it to work.

First, establish a MySQL connection:
$db = mysql_connect('db_host', 'db_user', 'db_password');
Then, use $db variable with the mysql_query() function instead of $this->db->get:
$result = mysql_query($db, 'SELECT * FROM Meetings INNER JOIN Minutes ON Minutes.meeting_id = Meetings.meeting_id WHERE Rooms.date >= "' . $from . '" AND Rooms.date <= NOW() AND Minutes.meeting_id = $id')
Or even without the $db variable (if you use only one database):
$result = mysql_query('SELECT * FROM Meetings INNER JOIN Minutes ON Minutes.meeting_id = Meetings.meeting_id WHERE Rooms.date >= "' . $from . '" AND Rooms.date <= NOW() AND Minutes.meeting_id = $id')

Related

Mysql Query Getting Item as ID in 2 different tables

Im sorry for my bad english but this is the best i can.
I am trying to make a script that get's the item values of one line in my
for this example i'll be using the row with ID 2.
The first query is working correctly and getting the 2 values of 1items and 2items and deletes the ; that comes with it.
But the second part seems a little bit harder and i can't solve it.
The query is getting id, base_item from the table
The last 2 Outputs marked red are the one's that are matching 1items & 2items
I'm trying to let the if statement filter the $item1 as id in the item table and output the baseitem of the found row
same goes for item2
I'm using MySQL & MySQLi because this cms doesnt support newer versions of PHP yet.
<?php
$query = "SELECT * FROM logs_client_trade ORDER by id DESC";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo("<tr>");
echo("<td>" . $row['id'] . "</td>");
echo("<td>" . $row['1id'] . "</td>");
echo("<td>" . $row['2id'] . "</td>");
$userinfo = "SELECT id, base_item FROM items LIMIT 1";
$result2 = mysql_query($userinfo) or die(mysql_error());
while($row2 = mysql_fetch_assoc($result2)){
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
}
$tradetime = $row['timestamp'];
$date = date("d M Y - H:i:s", $tradetime);
echo("<td>$date</td></tr>");
}
?>
You forgot the while loop for the second query:
$userinfo = mysql_query("SELECT id, base_item FROM items");
while($get2 = $userinfo->fetch_assoc()) {
$baseid = $get2['id'];
$baseitem = $get2['baseitem'];
echo("<tr>");
[...]
}
In addition to that, your code is a mess. In the second loop you are still listing the rows from the logs table... I suggest you to review carefully your code, being careful on how you are using the different $row and $get2 arrays.
You can use a JOIN to get all of the info with one SQL statement:
SELECT *
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
UPDATE:
Here's the PHP code that will run the query and output the table rows. I've specified the column names based on the code in the original question.
<?php
$query = "SELECT
a.`id`,
a.`1id`,
a.`2id`,
a.`1items`,
a.`1items`,
IFNULL(b.`baseitem`,'Not Available') as `baseitem1`,
IFNULL(c.`baseitem`,'Not Available') as `baseitem2`,
DATE_FORMAT(a.`timestamp`,'%d %m %Y - %H:%i:%s') as `tradetime`
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo "<tr>\r\n";
echo " <td>" . $row['id'] . "</td>\r\n";
echo " <td>" . $row['1id'] . "</td>\r\n";
echo " <td>" . $row['2id'] . "</td>\r\n";
echo " <td>" . $row['baseitem1'] . "</td>\r\n";
echo " <td>" . $row['baseitem2'] . "</td>\r\n";
echo " <td>" . $row['tradetime'] . "</td>\r\n";
echo("</tr>\r\n");
}
?>

Print column value if one column name is same in all tables using left join

How to echo any column value by using LEFT JOIN method where one column name is common in all the tables used in select query? Below in my code. I want to get 'app_id' value along with all the columns, but 'app_id' doesn't print anything. app_id column is available in all 4 tables used in this query. It prints all the rows that i want to show but app_id does not return any value.
<?php
session_start();
include('../../connect.php');
$date = strtotime("+1 day", time());
$cdate = strtotime("-7 day", time());
$finaldate = date('Y-m-d', $date);
$scdate = date('Y-m-d', $cdate);
//$sql = "SELECT * FROM tblapps WHERE journeydate >= '".$finaldate."' AND appstatus = 'On-Hold' AND process = 'yes'";
$sql = "SELECT * from tblapps
LEFT JOIN tblapps2 ON tblapps.app_id = tblapps2.app_id
LEFT JOIN tblapps3 ON tblapps.app_id = tblapps3.app_id
LEFT JOIN payments ON tblapps.app_id = payments.app_id
WHERE
tblapps.journeydate >= '$finaldate' AND tblapps.appstatus = 'On-Hold' AND tblapps.process = 'yes'";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["app_id"]. " - Name: " . $row["firstname"]. " " . $row["journeydate"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

Run 2 queries together but execute separately

I currently have 2 separate queries which i need to run together, but execute separately, and i was wondering what would be the best practice to do this.
I have a query that dynamically prints out restaurants each and every time one is added in the admin page, and i have a query that works out if the restaurant is open or closed. I thought this would be simple by executing the 2 separate queries then saving the opening times echo into a variable and putting this variable inside the loop, however, that does not seem to be working. By not working I mean the wrong id is being called as the opening hrs query is before the dynamic print out query, and when it is after the query works the plan, but then my echo/print out variable does not work. I am struck and have no idea how to move forward.
Opening hr query
$query = mysqli_query($dbc, "SELECT * FROM Opening_hrs
WHERE Restaurant_ID='$rest_id' AND Day_of_week = DATE_FORMAT(NOW(), '%w')
AND CURTIME() BETWEEN Open_time AND Closing_time");
$run_qu = $dbc->query($query);
if($run_qu->num_rows>0){
while($row_qu=$run_qu->fetch_assoc()){
$message= "open" .$row_qu["Open_time"]."</br>";
}
} else {
$message=$message. "close".$row_qu["Closing_time"]."</br>";
}
Dynamic query
$sql = mysqli_query($dbc, "SELECT Rest_Details.Resturant_ID, Rest_Details.Resturant_name,,Delivery_Pcode.Pcode
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE '%" . $pcode . "%'") or die("could not search!");
echo var_dump($sql);
$count = mysqli_num_rows($sql);
if ($count === 0) {
$output = '<b>we dont deliver to ' . $pcode . '</b></br>';
} else {
$i = 1;
}
while ($row_prods = mysqli_fetch_array($sql)) {
$rest_id = $row_prods['Resturant_ID'];
$rest_name = $row_prods['Resturant_name'];
$output = $output . '<div id="products">' .
' <p id="rest_name">' . $rest_name . '</p>' .
'<p> '.$message.' </p>' .;
$i++;
}
}
You should join the two queries:
SELECT
Rest_Details.Resturant_ID,
Rest_Details.Resturant_name,
Delivery_Pcode.Pcode,
Opening_hrs.Open_time,
Opening_hrs.Closing_time
FROM Rest_Details
JOIN Deliver_Pcode ON Delivery_Pcode.Restaurant_ID=Rest_Details.Restaurant_ID
LEFT JOIN Opening_hrs ON Opening_Hrs.Restaurant_ID=Rest_Details.Restaurang_ID
AND Day_of_week = WEEKDAY(NOW()) AND CURTIME() BETWEEN Open_time AND Closing_time
WHERE Delivery_Pcode.Pcode LIKE '%$pcode%'
Because it's a LEFT JOIN, Open_time and Closing_time will be filled in if the restaurant is open, otherwise they will be NULL. So the PHP that displays the results can check this:
while ($row_prods = mysqli_fetch_array($sql)) {
$rest_id = $row_prods['Resturant_ID'];
$rest_name = $row_prods['Resturant_name'];
$output .= '<div id="products">' .
' <p id="rest_name">' . $rest_name . '</p>';
if ($row_prods['Open_time']) {
$output .= '<p> open ' . $row_prods['Open_time'] . ' close ' . $row_prods['Close_time'];
}
$output .= "</div>";
$i++;
}
If you want to have a list of all restaurants which will deliver within a certain range of plzs and their opening hours then you could use this query:
$sql = '
SELECT
Rest_Details.Resturant_ID,
Rest_Details.Resturant_name,
Delivery_Pcode.Pcode,
EXISTS(
SELECT *
FROM Opening_hrs
WHERE
Restaurant_ID=' . $rest_id . ' AND
Day_of_week = DATE_FORMAT(NOW(), "%w") AND
CURTIME() BETWEEN Open_time AND Closing_time
) as isOpen
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE "%' . $pcode . '%"
';
Mysql exist() will return 1 if one or more rows match the the specified query and 0 otherwise. as isOpen will make this value accesibble in column with name isOpen.

Calculating total hours out of database

I have a Log in PHP which shows all the things I've done.
the construction is like:
Date -- Things done -- Hours
I figured how to show all the info in a table. But I want to have a total hours in the bottom which calculated all the hours of the database.
I'm adding every day a new log (what I've done that day). But I wanna know how I can do this. I've been searching for a while now, without success..
I hope anyone can help me.
Thanks in advance.
This is how I display my code(short version):
<tr>
<td class="date"><?php echo $row['Date'] ?></td>
<td><?php echo $row['Detail'] ?></td>
<td><?php echo $row['Hours'] ?></td>
</tr>
So for clarification:
I make a new td which echo's the total hours by adding all the hours.
So if my hours are 8 + 8 + 8 + 8 + 4 + 5 in the hours table it will be shown as:
Total hours: 41 hours
To display the total sum of hours, you do this:
$db = new mysqli( DB_HOST, DB_USER, DB_PASS, DB_DATABASE );
$sql = 'SELECT SUM( `Hours` ) AS `total` FROM `table_name`';
$res = $db->query( $sql ) or die( 'MySQL error ' . $db->error );
$row = $res->fetch_assoc();
echo 'Total: ' . $row['total'];
If date is of typ datetime/date/timestamp you can get the total for every day like this (If the date is of type date, you do not need DATE(date)):
$sql = 'SELECT DATE(`date`) AS `day`, SUM(`Hours`) as `total` '
. 'FROM `table_name` GROUP BY DATE(`date`) ORDER BY `date`';
$res = $db->query( $sql ) or die( 'MySQL error ' . $db->error );
while( $row = $res->fetch_assoc() )
echo $row['day'] . ' has ' . $row['total'] . ' hours' . PHP_EOL;
If date is of typ int or PHP timestamp you can get the total for every day like this:
$sql = 'SELECT DATE( FROM_UNIXTIME(`date`) ) AS `day`, SUM(`Hours`) as `total` '
. 'FROM `table_name` GROUP BY DATE(FROM_UNIXTIME(`date`)) ORDER BY `date`';
$res = $db->query( $sql ) or die( 'MySQL error ' . $db->error );
while( $row = $res->fetch_assoc() )
echo $row['day'] . ' has ' . $row['total'] . ' hours' . PHP_EOL;

Is there a way how you can only get datasets between two dates?

I was wondering if there is a way to only get Datasets between two dates (to be specific hours example: between 5pm and 6pm). I would have to make an Select and it only should return the datasets between 5 and 6 pm. Is there a way where you can do this easily?
Edit: here is the SQL code:
Select fahrten_id, StartOrt, Zielort, abfahrt FROM fahrten WHERE ' + $von + ' <= abfahrt AND abfahrt <= ' + $bis + '
the "abfahrt" is the date which should be checked.
Edit2:
<?php
header('Content-type: application/json');
$server = "---";
$username = "---";
$password = "---";
$database = "---";
$von = $_POST["von"];
$bis = $_POST["bis"];
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$sql = "Select fahrten_id, StartOrt, Zielort, abfahrt FROM fahrten WHERE '$von' <= abfahrt AND abfahrt <= '$bis'";
$result = mysql_query($sql) or die ("Query error: " . mysql_error());
$records = array();
while($row = mysql_fetch_assoc($result)) {
$records[] = $row;
}
mysql_free_result($result);
mysql_close($con);
echo $_GET['jsoncallback'] . '(' . json_encode($_POST) . ');';
?>
Here is the PHP from the code, i only removed the pw and username and servername
You can easily extract the hour from the date field. Then just limit the result to the hour(s) you want.
where Hour( DateField ) >= 17 and Hour( DateField ) < 18
This will pass any date where the time field is "17:00:00.00000" to "18:59:59.99999" (or however far the millis go). If you want an inclusive result, change the comparison:
where Hour( DateField ) between 17 and 18
This will include times that are exactly "18:00:00.00000". (It could happen!)

Categories