I am an amateur programmer and have recently been facing a challenge.
I am trying to select a data between range of dates but despite numerous attempts have been unsuccessful. Can someone help me with the code of pulling up data between date ranges.
My code is:
<?php
$tdate = $_POST['toDate'];
$fdate = $_POST['fromDate'];
mysql_connect("localhost","user","pass") or die("Couldn't connect!");
mysql_select_db("db_name") or die("Couldn't find db");
$data = mysql_query("SELECT * FROM db_table BETWEEN saledate '$tdate' AND '$fdate' ");
$result = mysql_fetch_array($data);
if (!$result) {
echo "No result";
} else {
echo $result;
}
?>
You shouldn't do Query like that. Use PDO.
Regarding your SQL is wrong. The right is:
$data = mysql_query("SELECT * FROM db_table WHERE saledate BETWEEN '$tdate' AND '$fdate' ");
You sql should be this
SELECT * FROM db_table WHERE saledate BETWEEN $tdate AND $fdate
In your code, instead of
$result = mysql_fetch_array($data);
if (!$result) {
echo "No result";
} else {
echo $result;
}
write the following code.
while($result=mysql_fetch_array($data))
{
echo $result['Fieldname1'];
......
......
echo $result['Fieldnamen'];
}
In place of fieldname, write the fields from your table. The fields you want to display.
Related
Hello what I want is to get the same $typeValue which I am displaying in a message and then display the data.
In the first page the code that I am display the message with a specific $typeValue is the below
//ALERT SYSTEM
$query2 = mysql_query("SELECT username, typeValue FROM sensors WHERE (sensorValue < min OR sensorValue > max) AND doctorStatus='0'");
$query3 = mysql_query("SELECT username FROM sensors WHERE (sensorValue < min OR sensorValue > max) AND doctorStatus='0'");
//$row3 = mysql_fetch_array($query3);
while($row = mysql_fetch_array($query2))
{
while($row3 = mysql_fetch_array($query3))
{
$p1 = mysql_num_rows(queryMysql("SELECT * FROM connected
WHERE username='$row3[0]' AND friend='$username'"));
$p2 = mysql_num_rows(queryMysql("SELECT * FROM connected
WHERE username='$username' AND friend='$row3[0]'"));
if (($p1 + $p2) > 1)
{
$alert_message= " <b><font color=red><p align='center'>User " . $row['username'] . " Has A Health Problem with his/her ".$row['typeValue']."</font></b>";
$link_address = "health_problem.php?view=".$row['username']."&typevalue=".$row['typeValue'];
?>
<?php echo $alert_message; ?>
<?php
}
}
}
And the code of the other page which I want to display this data
<?php
/**
* #author Nick Bourlai
* #copyright 2015
*/
include_once 'header.php';
$result = queryMysql("SELECT * FROM patient WHERE username='$username'");
if(mysql_num_rows($result))
{
$query = "UPDATE sensors SET status='1' WHERE status ='0' AND username='$username'";
mysql_query($query)or die(mysql_error());
}
else
{
$query = "UPDATE sensors SET doctorStatus='1' WHERE doctorStatus ='0' AND username='$view'";
mysql_query($query)or die(mysql_error());
}
$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$view = ($_GET['view']);
$username2 =$_SESSION['username'];
$typeValue = ($_GET['typeValue']);
$sql="SELECT typeValue,unit,sensorValue,datetime FROM sensors WHERE username='$view' AND typeValue='$typeValue'";
$result = mysqli_query($con,$sql);
echo $view;
echo $typeValue;
echo "<table>
<tr>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr> <b>Type: </b>";
echo stripslashes($row['typeValue']) . "<br/><b>Unit: </b>";
echo stripslashes($row['unit']) . "<br/><b>Value: </b>";
echo stripslashes($row['sensorValue']) . "<br/><b>Date: </b>";
echo stripslashes($row['datetime']) . "<br/>";
echo "--------------------------------------------------------------------------------------------------------------";
echo "<br/></tr>";
}
echo "</table>";
?>
So what I want is to get from the clicked link the specific row and display the data of this row in the other page.
#Fred-ii- by mistake i switch my language in the keyboard and you answer was correct , about the small V in the word please write it as an answer in order to accept it
As per requested:
Your GET value is using an lowercase v in the URL
health_problem.php?view=magda&typevalue=Temperature, rather than an uppercase V in relation to typeValue in the GET array $_GET['typeValue']
health_problem.php?view=magda&typeValue=Temperature
Variables are case-sensitive.
Plus, on top of what has already been stated by myself in comments, that different MySQL APIs do not intermix with each other. Use the same MySQL API from connection to query.
Footnotes:
Consider using mysqli with prepared statements, or PDO with prepared statements, they're much safer.
PHP variables and array keys are case sensitive. This line in your 2nd script
$typeValue = ($_GET['typeValue']);
should be
$typeValue = ($_GET['typevalue']);
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();
}
I'm creating a web page which has events with different dates which are printed out from my database using php. I want it so the events which have gone past the current date automatically do not show on the page.
I'm using the 'date' type in mySQLi for holding the date and when i'm inserting the date into my database i'm using the code;
<?php
if($db_server){
$eventdate = clean_string($db_server, $_POST['eventdate']);
$timeDate = strtotime($eventdate);
$tempdate = date("Y-m-d", $timeDate);
$query = "INSERT INTO events (eventdate) VALUES ('$tempdate')";
mysqli_query($db_server, $query) or
die("Insert failed. ". mysqli_error($db_server));
$message = "Event Uploaded";
}else{
$message = "Error: could not connect to the database.";
}
?>
Event Date: <input type="event-upload" class="standard" name="eventdate" id="datepicker" />
Here's the code when i'm printing out the date;
<?php
if($db_server){
$query = "SELECT * FROM events";
$result = mysqli_query($db_server, $query) or die(mysqli_error($db_server));
if (!$result) die('Query failed: ' . mysqli_error($db_server));
while($row = mysqli_fetch_array($result)){
echo $row['eventdate']
}
}
?>
If someone could help me out it and tell me what the right WHERE clause is to use, it would be much appreciated.
$query = "SELECT * FROM events WHERE eventdate >= '".date("Y-m-d")."'";
Is that what you're looking for?
Use this using DATEDIFF function in mysql
select * from `events` where DATEDIFF(`eventdate`,now() ) > 0
I know that $wins should be 3. Because I have 3 rows with the integer "1" in the "win" column on table "rated_teams" But for some reason this code will not work. Can you find the problem please? ALSO, I'm aware that some of this is depracted. I'll update the whole page, once I get it at least in working condition.
<?php
$sql = "SELECT SUM(win) FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con)
or die('A error occured: ' . mysql_error());
while ((mysql_fetch_array($query))) {
$wins = $row['SUM(win)'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Try with
$sql = "SELECT SUM(win) as sum FROM rated_teams WHERE server='$server' AND name='$myteam'";
and while you are getting give like
while ($row = mysql_fetch_array($query)) {
$wins = $row['sum'];
}
And my advice is try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
You do not set the $row variable. Edit your while to this.
while ($row = mysql_fetch_array($query))
You need to give your calculated column an alias. Try this:
<?php
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='$server' AND name='$myteam'";
$query = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
while ($row = mysql_fetch_array($query)) {
$wins = $row['sumwin'];
}
?>
<h3>Total Wins: <?php echo $wins?> </h3>
Please write sql query right manner.Write like this.
$sql = "SELECT SUM(win) as sumwin FROM rated_teams WHERE server='".$server."' AND name='".$myteam."'";
while ((mysql_fetch_array($query))) {
should be
while ($row = mysql_fetch_array($query) ) {
I have run across a problem during my query service to add a row in an online database in PHP. The addition of the row works just fine. I get user id and book id from the url and fetch the names of the book and the user to put into the row which i add to my third and last table.
When I get the names, put them in an array, json encode it and then echo it, it works. But when I put them in the row it prints resource id#3 and resource id#4 instead of the names.
Any ideas?
Here is my service:
<?php
$con = mysql_connect("localhost","root","root");
$userid=$_GET['uid'];
$id = $_GET['bookid'];
$type = $_GET['type'];
$zero = '0';
$one = '1';
$date = date("Y-m-d");
$arr = array();
if (!$con)
{
die('Could not connect: ' . mysql_error());
echo "error connection";
}
mysql_select_db("Jineel_lib",$con) or die("Could not select database");
$bkName = mysql_query("SELECT Name from books where ID='".$id."'");
$userName = mysql_query("SELECT Name from people WHERE User_ID='".$userid."'");
while($obj = mysql_fetch_object($userName))
{
$arr[] = $obj;
}
echo json_encode($arr);
if($type == 'borrow')
{
$query="UPDATE books set Availablity = '".$zero."' where ID= '".$id."' ";
mysql_query($query) or die (" borrow operation failed due to query 1");
$query1="INSERT into borrowed (BookID, BookName, BorrowerID, BorrowedName, DateBorrowed, Extended, Returned) values('".$id."','".$bkName."','".$userid."','".$userName."','".$date."','".$zero."','".$zero."')";
mysql_query($query1) or die (" borrow operation failed to due query 2");
echo "borrow success";
}
else if($type=='return')
{
$query="UPDATE books set Availablity = '".$one."' where ID= '".$id."' ";
mysql_query($query) or die (" return operation failed");
$query1="UPDATE borrowed set Returned = '".$one."' where BookID= '".$id."' ";
mysql_query($query1) or die (" return operation failed 1");
echo "return success";
}
else
echo "invalid parameters";
?>
THANK YOU IN ADVANCE
You don't actually retrieve the userName value here:
$userName = mysql_query("SELECT Name...
$userName is just the result resource object returned from the query. You do use mysql_fetch_object later on, which is appropriate, but then you try to use the actual result resource in your insert query:
$query1="INSERT into borrowed ...
It gets converted to the string you see. Instead, you need to use $obj->Name (you fetch the result into $obj, and presumably there is only one result). If there is more than one possible result, you will have to do that in a loop.
Listen to all of the comments on your question.