Timestamp Comparsion MySQL PHP - php

I´m trying to compare the rows, with the current date and hour,
$curDate = date("Y-m-d H").':00:00';
$query = "SELECT Id FROM Programacao WHERE Data1 = $curDate OR Data2 = $curDate OR Data3 = $curDate
OR Data4 = $curDate OR Data5 = $curDate OR Data6 = $curDate OR Data7 = $curDate";
$result = mysql_query($query);
if(!$result) {echo 'No program today';}
while ($row = mysql_fetch_array($result))
{
echo $row['Id'];
}
The row data in the database is like that: "2011-09-10 18:00:00"
But i always get : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
any corrections?

You need quotes round your date literals in SQL:
"... WHERE Data1 = '$curDat' ..."
The resulting SQL should look like something like this:
... WHERE Data1 = '2010-12-17 15:00:00' ...

$result = mysql_query($query) or die(mysql_error());
will give you error needed to know what is wrong
in your case, you've forgotten quotes around dates
this one is better and will probably work
$query = "SELECT Id FROM Programacao WHERE Data1 = '$curDate' OR Data2 = '$curDate' OR Data3 = '$curDate'
OR Data4 = '$curDate' OR Data5 = '$curDate' OR Data6 = '$curDate' OR Data7 = '$curDate'";

Related

Store data after mysql query

This is my problem. I have a table in my database in which there are two columns, ID and ANSWER. In this database there are more rows with same ID, so I want to save all the results with this id in an array.
the sql code is:
$sql6 = "SELECT * FROM Answer WHERE userId = :userId and qId = 'q6'";
$stmt6 = $conn->prepare($sql6);
$stmt6->execute(array(':userId' => $_SESSION['SESS_MEMBER_ID']));
now to fatch the results I don't know if I have to use
$result6 = $stmt6->fetch(PDO::FETCH_OBJ);
or
$result6 = $stmt6->fetchAll();
EX.
ID ANSWER
q6 abba
q6 bbaa
i would save in an array like this for example:
$array=("abba","bbaa");
then i would pass this array to a script in javascript.
You can either call $stmt->fetch in a loop, pushing each row onto the resulting array, or you can call fetchAll once -- it returns an array of all the rows. So the following are equivalent:
$array = array();
while ($row = $stmt6->fetch(PDO::FETCH_OBJ)) {
$array[] = $row;
}
and:
$array = $stmt6->fetchAll(PDO::FETCH_OBJ);
If you want an array with just the value of a single column, rather than objects representing the whole row, the loop will be easier:
while ($row = $stmt6->fetch(PDO::FETCH_OBJ)) {
$array[] = $row->q6;
}
#Barmar, your solution partially works. i've written this:
$sql6 = "SELECT * FROM Answer WHERE userId = :userId and qId = 'q6'";
$stmt6 = $conn->prepare($sql6);
$stmt6->execute(array(':userId' => $_SESSION['SESS_MEMBER_ID']));
$count6 = $stmt6->rowCount(array(':userId' => $_SESSION['SESS_MEMBER_ID']));
$answer6 = array();
while ($result6 = $stmt6->fetch(PDO::FETCH_OBJ)) {
$answer6[] = $result6->answer;
}
then i want to pass this array to a script, so:
<script>
var num = var ans6 = '<?php echo $count6; ?>';
var ans6 = '<?php echo json_encode($answer6); ?>';
for(var i=0; i<num;i++){
alert(ans6);
}
the result is a popup in which is written "["abba","bbaa"]".
but if i write
for(var i=0; i<num;i++){
alert(ans6[i]);
}
the popup shows first"[" and then " " "

MySQL search between 2 dates

I am having a wierd issue on a query, I am simply doing a search using 2 dates, I have all the dates in the database formatted in 2012-12-02 00:00:00 mysql format, but it is simply ignoring the AND cslblicstat = '10' AND wcxdate BETWEEN '$date1' AND '$date2' and giving me anything that has the matching class.
$user = $session->username;
if(isset($_POST['carrier'])){
$carrier = $_POST['carrier'];
$class[] = $_POST['class'][0];
$date1 = $_POST['date1'];
$date1 = date("Y-m-d 00:01", strtotime($date1));
$date2 = $_POST['date2'];
$date2 = date("Y-m-d 59:59", strtotime($date2));
foreach( $class as $key){
$query = "SELECT * FROM leads WHERE class1 = '$key' OR class2 = '$key' OR class3 = '$key' OR class4 = '$key' OR class5 = '$key' OR class6 = '$key' OR class7 = '$key' OR class8 = '$key' OR class9 = '$key' OR class10 = '$key' OR class11 = '$key' OR class12 = '$key' AND user = '' AND wccompcode = '$carrier' AND cslblicstat = '10' AND wcxdate BETWEEN '$date1' AND '$date2' LIMIT 100";
$sellead = mysql_query($query)or die(mysql_error());
while($leads = mysql_fetch_array($sellead)){
$arrayl[] = $leads;
$rowid = $leads['ID'];
$update = mysql_query("UPDATE leads SET user = '$user' WHERE ID = '$rowid'")or die(mysql_error());
}
}
}
In SQL, AND operator has higher precedence than OR. I guess that you just need to put the ORed conditions into parentheses:
SELECT *
FROM leads
WHERE (class1 = '$key' OR class2 = '$key' OR ... OR class12 = '$key')
AND user = ''
AND wccompcode = '$carrier'
AND cslblicstat = '10'
AND wcxdate BETWEEN '$date1'
AND '$date2'
LIMIT 100
When in doubt about precedence of operators, use parenthesis (or consult the manual).
You could also write that condition with IN:
WHERE '$key' IN (class1, class2, ... , class12)
AND user = ''
---
In MySQL AND is evaluated before OR. So your WHERE clause is equivalent to this:
SELECT 1 OR 0 AND 0;
The result is true:
+--------------+
| 1 OR 0 AND 0 |
+--------------+
| 1 |
+--------------+
Also, a syntactically simpler method of expressing your OR conditions would be something like this:
WHERE '$key' IN ( class, class2, class3, ... ) AND ... ;
But anytime you have columns like that with numbers, what it really means is that your schema can be improved.

TableName of the result query

When the result of a query exists, I want to know the name of the row(s) that have the correct results:
$query = "SELECT Id FROM Programacao WHERE ID = 1";
$curDate = date("Y-m-d H").':00:00';
$query = "SELECT Id
FROM Programacao
WHERE Data1 = '$curDate'
OR Data2 = '$curDate'
OR Data3 = '$curDate'"
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){}
simply talking its if(Data1 == curDate)return Data1; if(Data2 == curDate)return Data2....
Sorry for my bad english.
Well it would be Programacao from your query. But if you needed it dynamically you could try functions like mysql_table_name() or mysql_field_name().
A select statement does not go into a table.
It goes into a resultset.
Resultsets do not have names, they just are.
http://en.wikipedia.org/wiki/Result_set
Perhaps you mean this? (either with UNION or UNION ALL):
$query = "
SELECT Id
, 'Data1' AS name
FROM Programacao
WHERE Data1 = '$curDate'
UNION
SELECT Id
, 'Data2'
FROM Programacao
WHERE Data2 = '$curDate'
UNION
SELECT Id
, 'Data3'
FROM Programacao
WHERE Data3 = '$curDate'
" ;

10 most recent rows from mySQL by time and define variables

I want to get the 10 most recent rows from a mysql database.
How can I do that?
+----+-------+----+-----------+
| id | value | ap | timestamp |
+----+-------+----+-----------+
And define variables for value, ap, and timestamp... for each row!
like:
$value1 = ?;
$ap1 = ?;
$timestamp1 = ?;
$value2 = ?;
$ap2 = ?;
$timestamp2 = ?;
etc....
The simplest way is probably something like this:
$db = new PDO('mysql:host=<hostname>;port=<port>;dbname=<database name>', '<username>', '<password>');
$query = 'SELECT id, value, ap, `timestamp` '.
'FROM <table> '.
'ORDER BY `timestamp` DESC '.
'LIMIT 10';
$resultset = $db->query($query);
$results = $resultset->fetchAll();
Then you will have a two-dimensional array with 10 sets of the values you want. $results[0]['id'], $results[0]['value'], $results[0]['ap'], and $results[0]['timestamp'], up through $results[9]['id'], etc.
You can get the data like so..
$query = mysql_query("SELECT * FROM `my_table` ORDER BY `timestamp` DESC LIMIT 10");
and you can use the results like so..
while($results = mysql_fetch_object($query)) {
echo $results->value;
echo $results->ap;
echo $results->timestamp;
}

Php loop need help?

This is sql table
id id_user id_userfor description date
1 231 119 a to b 2010-02-05 17:42:47
2 231 231 myself 2010-02-05 17:36:28
3 231 119 from x to b 2010-02-05 17:36:29
I could not find the way to select output result which looks something like this:
user 119
a to b 2010-02-05 17:42:47
from x to b 2010-02-05 17:36:29
user 231
myself 2010-02-05 17:36:28
You can use
select id_userfor, description, date from table order by id_userfor;
then you can print all the date column data for each distinct id_userfor
$old = '';
while($row = mysql_fetch_array($result)) {
if($old != $row['id_userfor']) {
$old = $row['id_userfor'];
echo "$row['id_userfor']<br />";
}
echo $row['description'] . " " . $row['date'] . "<br />";
}
You can query with the id_userfor field sorted Then loop all record and everytime you encounter a new value of id_userfor print it.
Something like this:
$SQL = 'select * from ... order by `id_userfor`';
$Result = ...;
$PrevUserFor = '';
while($Row = ...) {
const $UserFor = $Row['id_userfor'];
if ($PrevUserFor != $UserFor) {
// Here is where you print the user ID
echo "user $UserFor<br>";
$PrevUserFor != $UserFor;
}
$Description = $Row['description'];
$Date = $Row['date'];
echo(' $Description $Date<br>');
}
Hope this helps.
The codaddict query a little more readable:
SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`
And then, inside the loop, you'll use something like this:
$sql = "SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`";
$query = mysql_query($sql);
$user = null; // Empty user
while($data = mysql_fetch_assoc($query)) {
if ($data['id_userfor'] != $user) {
// Display the user only when it changes
echo '<H1>'. $data['id_userfor'] .'</H1>';
$user = $data['id_userfor'];
}
// Display the rest of the fields here :)
}

Categories