Were can i insert my datediff query in this code
$search = $_POST['search'];
$result = mysql_query("SELECT * FROM documents where docnum "My Search Bar"='$_SESSION[test2]' and doctitle LIKE '%$search%' order by docprim desc");
while($row = mysql_fetch_array($result))
{
echo "<tr class ='hovered'>";
echo "<td width = '100px' class='text-left'>";
echo $row['docnum'];
echo "</td>";
echo "<td width = '100px' class='text-left'>";
echo $row['datein']." to ".$row['dateout'];
echo "</td>";
}
this is a sample of my code. i want to add duration from datein and dateout field from my table.
You need to modify your SQL Query String.
$startdate = date('Y-m-d');
$enddate = date('Y-m-d', strtotime("-3 days"));
$result = mysql_query("SELECT * FROM documents where docnum ='".$_SESSION['test2']."' and doctitle LIKE '%$search%' and DATEDIFF('".$startdate."', '".$enddate."') > 0 order by docprim desc");
Also your SQL Query string has issues. Let me know if you need help with that as well.
Hope this helps.
Insert TIMESTAMPDIFF before '*' in sql-query:
SELECT TIMESTAMPDIFF(SECOND, d.datein, d.dateout) AS timediff, m.* FROM documents m;
and use as:
echo $row['datein']." to ".$row['dateout'] . " (" . $row['timediff'] . ")" ;
$row['timediff'] will show seconds.
Related
I am trying to find if two runners have ever ran in the same race. The two runners are Peter Smith and Diane Peters.
$resultRaceType = mysqli_query($db,"SELECT DISTINCT date,time FROM results where runner = 'Peter, Smith' ");
while($row = mysqli_fetch_array( $resultRaceType ))
{
$resultRaceType1 = mysqli_query($db,"SELECT * FROM results where date = ' " . $row['date'] . " ' and time = ' " . $row['time'] . " ' and runner = 'Diane, Peters'");
while($row1 = mysqli_fetch_array( $resultRaceType1 ))
{
echo "<tr >";
echo "<td>";
echo $row1['date'];
echo " - " . $row1['time'];
echo "</td>";
echo "<tr>";
}
}
The above code works, but only if I limit the first select to LIMIT 50. So I can see that it is timing out. My table has over 100K rows. I know I am doing something wrong but cant see what it is.
Thanks for any help you guy's can give me.
Try:
SELECT a.date, a.time FROM results a
JOIN results b ON (a.date = b.date AND a.time = b.time)
WHERE a.runner='Peter, Smith' AND b.runner='Diane, Peters';
Hi am retreiving the distinct dates then am trying to retreive the rows with that dates but am getting them as zero..
Here is code
$sql = "SELECT DISTINCT date FROM video_data ORDER BY id DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$date = $row['date'];
echo "<tr>";
echo "<td>$date</td>";
$query_order = "SELECT * FROM video_data WHERE date=$date";
$query_order_total = mysqli_query($connection,$query_order);
var_dump($query_order);
$total = mysqli_num_rows($query_order_total);
echo "<td>$total</td>";
echo"<tr>";
But when am selecting "SELECT * FROM video_data" its showing correct count
you are missing ' ' around $date
$query_order = "SELECT * FROM video_data WHERE date='$date'";
That said: when you have, let's say, 60 dates found in your first query, you will than fire 60 new queries to find the actual data.
how about:
$sql = "SELECT COUNT(*) total, date
FROM video_data
GROUP BY date
ORDER BY date DESC";
$result = mysqli_query($connection, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>". $row['date'] ."</td>";
echo "<td>". $row['total']. "</td>";
echo"<tr>";
}
In mysql I want to display matched information with values... But the code I use select information separately and I get a bad result. What I want is to wrap the "select" functions together so they both look for specific information in mysql database.
Here's the code I use:
$name = explode(',',$name); //splits search
$query = "SELECT * FROM lucky WHERE name LIKE '%" . implode("%' AND name LIKE '%", $name) . "%'";
$sname = explode(',',$sname); //splits search
$query = "SELECT * FROM lucky WHERE sname LIKE '%" . implode("%' AND sname LIKE '%", $sname) . "%'";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
echo "<table id='box'>";
echo "<tr;>";
echo "<td id='text''>Name:</td><td id='haha'>" . $row['name']. "</td>";
echo "<td id='text'>Second Name:</td><td id='haha'>" . $row['sname'] . "</td>";
echo "</tr;>";
echo "</table>";
} else { echo "No mathed information" ; }
Thanks in advance :)
UPADATE
Thanks to Spencer for his help!!!
Here's the code that I use now. Please use this to select matched information from the database!!!
$query = SELECT *
FROM lucky
WHERE name LIKE '$name'
AND sname LIKE '$sname'
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()) {
echo "<table id='box'>";
echo "<tr;>";
echo "<td id='text''>Name:</td><td id='haha'>" . $row['name']. "</td>";
echo "<td id='text'>Second Name:</td><td id='haha'>" . $row['sname'] . "</td>";
echo "</tr;>";
echo "</table>";
} else { echo "No mathed information" ; }
If your $sname string contains the value "jack,jill", then the query would only return rows that have a name column value that contains both of those strings, for example: 'jack and jill' and 'jillojellojacko' would match. But the query will not return rows where the name column contains 'jack' but doesn't contain 'jill'.
If your intent is to search for rows that have either of the values matching, for example
$name = 'fee,fi,fo'
$sname = 'fum'
That is, any rows where name column contains either 'fee', or 'fi', or 'fo', or sname column contains 'fum', you could use a query of the form:
SELECT t.*
FROM lucky t
WHERE t.name LIKE '%fee%'
OR t.name LIKE '%fi%'
OR t.name LIKE '%fo%'
OR t.sname LIKE '%fum%'
If you replace all those ORs with ANDs, then a row will need to satisfy all of those predicates to be returned. If you want a combination of AND and OR, then use parens to specify the order of precedence...
i currently have a table i need displayed like this:
but as you can see, its not sorted on "amount dropped" (named $amount and amount in the code)
i first need to gather the names and drop_id's from a table. which is done like this:
//----------------FETCH ALL CONTENTS FROM DROPTABLE TO DISPLAY DROPS------------\\
$query = "SELECT * FROM droptable
WHERE boss_id = ". $boss ."";
$stmt = $pdo->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row){
$drop_id = $row['drop_id'];
$drop = $row['dropname'];
$boss_id = $row['boss_id'];
$picture = $row['picture'];
this fetches the name of the drop(dropname). the id for it (drop_id), the boss id (boss_id) and the picture for it (picture).
it then goes on to check if ive ever logged a drop from the boss from another table:
//----------------FETCH ALL LOGGED DROPS FOR DISPLAYING AMOUNT------------\\
$query1 = "SELECT * FROM dropcounter
WHERE boss_id = ". $boss ." AND userid = ". $user ." AND drop_id = ". $drop_id ."";
$stmt1 = $pdo->prepare($query1);
$stmt1->execute();
$result1 = $stmt1->fetchAll(PDO::FETCH_ASSOC);
echo "<tr>";
echo "<td><img src='../images/". $picture ."'</td>";
echo "<td>". $drop . "</td>";
echo "<td>". $amount ."</td>";
echo "<td>". $percent ."</td>";
echo "<form action='logger.php' method='post'>";
echo "<td><input type='hidden' value=". $drop_id ." name='drop_id'>";
echo "<input type='hidden' value=". $boss_id ." name='boss_id'>";
echo "<input type='hidden' value=". $user ." name='user'>";
echo "<input type='submit' value='Add'></td>";
echo "</tr>";
echo "</form>";
this does everything i want exept sort on amount dropped or drop percentage (since they will sort the same). ive tried adding "ORDER BY amount DESC" in the 2nd query but it didnt sort.
ive also tried using JOIN, but it didnt come close to the result i wanted and i got stuck for 3 days on the query so went with the above code instead. but im willing to use join again if my wished result can be done.
here's the JOIN code that doesnt work:
$query = "SELECT dropcounter.drop_id, dropcounter.boss_id, dropcounter.add_date, dropcounter.username, dropcounter.amount, droptable.drop_id, droptable.dropname, droptable.boss_id, droptable.wiki_link, droptable.picture
FROM droptable
JOIN dropcounter
ON droptable.boss_id = dropcounter.boss_id
WHERE dropcounter.drop_id = droptable.drop_id AND droptable.boss_id = ". $boss ." AND dropcounter.username = ". $user ."
ORDER BY dropcounter.amount";
here's the structure i have on my tables:
DROPCOUNTER TABLE:
and here's DROPTABLE table:
if anyone would be able to help me with either one if them i would be very glad for your kindness!
here's a fiddle if anyone wanna try it out. ive imported some sample data: http://sqlfiddle.com/#!2/d66846/1/0
You need to use LEFT JOIN if you want to get rows from droptable that don't have a match in dropcounter. All the tests that refer to dropcounter have to be in the ON clause, otherwise the null matches will cause the tests to fail and those rows will be filtered out.
SELECT dc.add_date, dc.username, IFNULL(dc.amount, 0) amount, dt.drop_id, dt.dropname, dt.boss_id, dt.wiki_link, dt.picture
FROM droptable dt
LEFT JOIN dropcounter dc
ON dt.boss_id = dc.boss_id AND dc.drop_id = dt.drop_id AND dc.userid = $user
WHERE dt.boss_id = $boss
ORDER BY amount
DEMO
You're trying to use in joined statement:
dropcounter.username = ". $user ."
which contains 2 errors:
1. there isn't field username in that table
2. you haven't enclosed string in ''
Change it to dropcounter.userid
Also you don't need to select droptable.drop_id and droptable.boss_id as it's already selected from dropcounter table.
EDIT: Based on your fiddle:
SELECT dropcounter.drop_id, dropcounter.boss_id, dropcounter.userid, dropcounter.amount, droptable.dropname
FROM droptable
JOIN dropcounter
ON droptable.boss_id = dropcounter.boss_id
WHERE dropcounter.drop_id = droptable.drop_id AND droptable.boss_id = 1 AND dropcounter.userid = 1
ORDER BY dropcounter.amount DESC;
It works and sorts without problem.
I have a table with two collumns (shortened), NAME and CATEGORY.
I want to output the number of distinct categorys. For an examle: Sport : 5 , Houses : 10.
I use this one:
$test = mysqli_query($con,"SELECT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
This work then I run the code in SQL Shell, but I have no clue on how to output it in PHP. I have searced Google up and down without any successfull solution.
Any help?
I want to output it in a table format.
EDIT: Here is my full code: (tablename is changed, and $con is removed)
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
while($row = mysql_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
$test = mysqli_query($con,"SELECT DISTINCT lkategori, COUNT(lkategori) as count FROM tablename GROUP BY lkategori ORDER BY count DESC");
echo "<table border='1'>";
while($row = mysqli_fetch_array($test)) {
echo "<tr>";
echo "<td>" . $row['lkategori'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "</tr>";
}
echo "</table>";
This will output all the categories and the count returned by the sql statement into a table. Also as a sidenote you should look into PDO.
EDIT: to make sure you do get the distinct values you should use the DISTINCT keyword in your sql statement:
$test = mysqli_query($con,"SELECT DISTINCT category, COUNT(category) as count FROM tablename GROUP BY category ORDER BY count DESC");
use this
while($row = mysqli_fetch_array($test)) {
echo $row['lkategori'] . ":" . $row['count'];
die("test");
}
Thanks