I get result from first query correctly However, when I want to use them in second query in WHERE condition I get query error 1064. If I remove WHERE it will work fine. Also,when I try to echo variables inside while in second query code it will print.the variables will not work only in WHERE in second query
$queryDate = "SELECT date , time from DATES where ID = $ID";
$result = mysqli_query($connection, $queryDate);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$date = $row['date'];
$time = $row['time'];
}
}
$queryCom = "SELECT * from DATES, BOOKING where dates.time = $time and booking.IDofFullDate= $date";
$result1 = mysqli_query($connection, $queryCom);
if (!$result1)
{
die("Query Faile". mysqli_errno($connection));
}
if ($result1->num_rows >0) {
echo $date;
echo $time;
}
on your second query try this below:
$queryCom = "SELECT * from DATES as dates, BOOKING as booking where dates.time = $time and booking.IDofFullDate= $date";
Related
I want to show data,based on previous week.
my query for pg-admin is
"SELECT * FROM event WHERE datetime BETWEEN NOW()-INTERVAL '6 DAYS' AND NOW()"
it is working fine in pg-admin editor and shows me correct data,but when i implement this query to php code it doesn't show the required result.
my php code is
$queryStr ="SELECT * FROM event WHERE datetime BETWEEN NOW()-INTERVAL '6 DAYS' AND NOW()";
$result = pg_query($conn,$queryStr);
if (!$result) {
echo "An error occurred in query.\n";
exit;
}
$result = pg_fetch_assoc($result);
return $result;
it shows me only one row any solution?
use something like below :
while ($row = pg_fetch_assoc($result))
{
echo $row['id'];
echo $row['author'];
echo $row['email'];
}
I want to echo each car ID which is available in a period between the start_date and the end_date. So far i got this with some help here on SO. This gives me a page not responding, if I delete the second part (commented in the script down here) it is eching all the car id's but not only the ones which are available ofcourse. What is wrong with the script? Because i cant figure out what i did wrong here.
<?php
$start_date = '2017-06-12';
$end_date = '2017-06-14';
$items = array();
$result = mysqli_query($con, "SELECT * FROM invoice_line ");
while ($car = mysqli_fetch_array($result)) {
echo $car['car_car_id'];
//second part
$car_available = mysqli_query($con, "SELECT *
FROM invoice_line
WHERE car_car_id = $car['car_car_id']
and start_date>='$start_date'
and end_date<='$end_date'");
echo $car_available['car_car_id'];
} // end second part
?>
The process you are doing is:
1. fetch all lines from invoice_line
2. for each line, fetch all lines between two dates.
You do it wrong, because you do not have to use two queries. You can use one:
<?php
$start_date = '2017-06-12';
$end_date = '2017-06-14';
$result = mysqli_query($con, "SELECT car_car_id FROM invoice_line where start_date>='$start_date' AND end_date<='$end_date'");
while ($car = mysqli_fetch_array($result)) {
echo $car['car_car_id'];
} // end second part
?>
I would add distinct for car_car_id, in order to get only one line per car id. if you want other data frome this line, you have to add it in the "SELECT" query
Will you try this code? Hope it may solve your problem you're echoing $car['car_car_id'] twice in your code but you missed the loop of your available cars
<?php
$start_date = '2017-06-12';
$end_date = '2017-06-14';
$items = array();
$result = mysqli_query($con, "SELECT * FROM invoice_line ");
$carid = null;
while ($car = mysqli_fetch_array($result)) {
$carid = $car['car_car_id'];
echo $carid;
//second part
$car = mysqli_query($con, "SELECT * FROM invoice_line WHERE car_car_id = '$carid' AND start_date>='$start_date' AND end_date<='$end_date'");
while ($car_available = mysqli_fetch_array($car)) {
echo $car_available['car_car_id'];
}
} // end second part
?>
So i want to get one and only the first value from a date column in my database, and put it in my PHP file, this is the snippet of the code to get the date:
$one = 1;
$Lihat="SELECT * FROM event where status = '$one'";
$Tampil = mysqli_query($db, $Lihat);
while ( $hasil = mysqli_fetch_array ($Tampil)) {
$query_start_date = "SELECT tgl_event
FROM jadwal_acara
WHERE id_event = '$id_event' AND status = $one
ORDER BY tgl_event ASC
LIMIT 1";
$result = mysqli_query($db, $query_start_date);
$start_date = mysqli_fetch_field($result);
<td class="tgl_mulai"><?php echo date('d-m-Y', strtotime($start_date));?></td>
The problem is that when i tried to echo it in my table row:
It returns an error that said:
strtotime() expects parameter 1 to be string, object given.
Does anyone know how to change it into a string or another method to get the desired display result? Thank you.
EDIT:
$start_date = var_dump($start_date); returns a NULL value when i tested it.
Notice: Undefined property: stdClass::$tgl_event in
NULL
But i got other rows that displayed the correct name and event ID, it's just the date that won't display correctly.
it now displays :01-01-1970
mysqli_fetch_field() returns an object. Instead use mysqli_fetch_assoc
Try this:
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT tgl_event FROM jadwal_acara WHERE id_event = ".$id_event." AND status = 1 ORDER BY tgl_event ASC LIMIT 1";
if ($result = mysqli_query($db, $query)) {
while ($finfo = mysqli_fetch_assoc($result)) {
$date = date('m-d-Y', strtotime($finfo['tgl_event']));
}
mysqli_free_result($result);
}
mysqli_close($db);
And then in your HTML:
<td class="tgl_mulai"><?php echo $date; ?></td>
I have the following code which should select a row from my DB with the current day's date.
$sql = "SELECT * FROM Forecast WHERE date(epoch) LIKE '". $mysqldate . "';";
echo $sql . "\n";
$result = mysqli_query($conn, $sql);
if (!$result || mysqli_num_rows($result) == 0) {
$row = mysqli_fetch_object($result);
$pop = 0;
$forecast = new DailyForecast($row->epoch, ...);
return $forecast;
}
else {
echo "no data found..\n";
}
Running SELECT * FROM Forecast WHERE date(epoch) LIKE '2015-11-03' in PHPmyAdmin works fine. In my script it return false or nothing...
Your condition looks like it's the wrong way round.
if (!$result || mysqli_num_rows($result) == 0) {
So if there's no result or no rows, you start executing code that seems to expect rows. However if there is a result or there are rows you echo 'no data found'
Try this instead:
if ($result && mysqli_num_rows($result) > 0) {
I have a database field which are
Appt_Datetime (which is call as DateTime in my table)
Svc_ID (which i call ApptType in my table)
I wanted the system to let the customer know that the datetime for the appt type is not available once someone else has book that slot.I have done a lot of research and trying out different codes but to no avail. I've seen answers on stackoverflow that uses PDO but im not so clear about it hence i'd like something to do with mysql. I have been stuck at with this at least few weeks now. Help
This is my call func:
$datetime = $_POST['DateTime'];
$appt = $_POST['ApptType'];
This is the query i last tried out but still is not working:
//Define query
$vquery = "SELECT * FROM Appointment where Appt_DateTime='$datetime' && Svc_ID='$appt'";
//Run Query
$result = mysql_query($query, $conn);
$row = mysql_fetch_assoc($result);
if($row==1)
{
echo "Date in not available";
}
else if($row==0)
{
$query = "INSERT INTO Appointment (Client_ID,Svc_ID,Appt_DateTime)
VALUES ('$_POST[ClientID]','$_POST[ApptType]','".date('Y-m-d H:i:s', strtotime($_POST[DateTime]))."')";
mysql_query($query,$conn);
}
Hint:
change this to $result = mysql_query($query, $conn); to $result = mysql_query($vquery, $conn);
at the time you are using $conn you do not have $query it is $vquery:
$result = mysql_query($vquery, $conn);
And as suggested above in comments better use mysqli or PDO.
you can try this condition..
//Define query
$vquery = "SELECT * FROM Appointment where Appt_DateTime='$datetime' && Svc_ID='$appt'";
//Run Query
if($result = mysql_query($vquery, $conn)){
//$row = mysql_fetch_assoc($result);
if(mysql_num_row($result)>0)
{
echo "Date in not available";
}
else
{
/* $query = "INSERT INTO Appointment (Client_ID,Svc_ID,Appt_DateTime)
VALUES ('$_POST[ClientID]','$_POST[ApptType]','".date('Y-m-d H:i:s', strtotime($_POST[DateTime]))."')";
mysql_query($query,$conn); */
echo "insert";
}
}else{
echo mysql_error();
}