i need to create a function that will display the articles of the current date only.
Here is what i have created:
function lastnews () {
$id = mysql_real_escape_string ($id);
$date = date('d');
$sql = 'SELECT * FROM posts WHERE post_status = "publish" AND post_date = "$date" ORDER BY post_date DESC LIMIT 3';
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) !=0):
while ($row = mysql_fetch_assoc($res)) {
$title = $row['post_title'];
echo '<li>'.$title.'</li>';
}
endif;
} // end
But this is not working. the formt of date in database is :
Y-m-d H:i
Thank you for reading this
You need to specify the entire date (date('Y-m-d')) and the query should say AND post_date > '$date' because it includes a time. You can also say AND post_date > CURRENT_DATE.
Change
$date = date('d');
To:
$date = date('Y-m-d H:i');
Related
I started my project March last year and have almost got it right.
Trying to get data from mysql and put it in highcharts.
This is my query
<?php
$con = mysqli_connect("localhost","root","root","manortsc_test");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$sth = mysqli_query($con,"
SELECT DateTime,max(T)
FROM alldata
WHERE DATE(DateTime) = CURDATE() - INTERVAL 1 DAY
GROUP BY hour(DateTime)
"
);
$rows = array();
$rows['name'] = 'Outside';
while($r = mysqli_fetch_array($sth)) {
$rows['data'][] = $r['max(T)'];
}
$result = array();
array_push($result,$rows);
print json_encode($result, JSON_NUMERIC_CHECK);
mysqli_close($con);
?>
The json output is missing the date and time
[{"name":"Outside","data":[17.5,16.3,15.6,15.1,14.4,14,14.1,16,18.5,21.7,24.1,26.9,28.3,29.6,30.6,31.1,31.8]}]
The graph shows okay (except datetime on x axis) and I cannot figure how to fix it. I have tried every way except the correct way. Any help would be appreciated.
Changed the query to this and now it works.
Thanks wergeld and Yuri for the assistance.
$sth = mysqli_query($con,"
SELECT DateTime,max(T)
FROM davisvp
WHERE DATE(DateTime) = CURDATE()
GROUP BY hour(DateTime)
"
);
$result = array();
$result['name'] = 'temperature';
while($row = mysqli_fetch_array($sth))
{
$date = strtotime($row['DateTime']);
$maxt = 1 * $row['max(T)'];
$result1 = array();
array_push($result1,$date);
array_push($result1,$maxt);
$result['data'][] = $result1;
}
echo json_encode($result, JSON_NUMERIC_CHECK);
I am using raw SQL in doctrine.
my SQL:
//input datetime type for $dateStart,$dateEnd
$start = date_format( $dateStart,'Y-m-d');
$end = date_format( $dateEnd,'Y-m-d');
$sql = '
SELECT
*
FROM
log
WHERE
time >= "? 00:00:00"
AND
time <= "? 23:59:59"
';
$pdo = $this->em->getConnection();
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1,$start);
$stmt->bindValue(2,$end);
$stmt->execute();
$rs = $stmt->fetchAll();
I tried bindValue, bindParam but no hope. I've also tried $param as array to pass to execute and it did not work.
$param = array($start,$end);
Could anyone help me?.
You cannot bind a part of a string literal, but a complete literal only
Besides, it makes no sense to split your date creation between PHP and SQL
$start = date_format( $dateStart,'Y-m-d 00:00:00');
$end = date_format( $dateEnd,'Y-m-d 23:59:59');
$pdo = $this->em->getConnection();
$stmt = $pdo->prepare('SELECT * FROM log WHERE time BETWEEN ? AND ?');
$stmt->execute([$start, $end]);
$rs = $stmt->fetchAll();
I have one problem.
I make search function in php. I need limit searching only for 1 mounth ago.
ex.
I put 'android' word
In result show only titles who containing android openet in last mounth. Oldest no show.
Thank you
EDIT
$sql = '
SELECT aa_search_index.content_id, MATCH(aa_search_index.title, aa_search_index.metadata) AGAINST (?) AS score
FROM aa_search_index
LEFT JOIN aa_thread
ON (aa_thread.thread_id = aa_search_index.content_id)
WHERE MATCH(aa_search_index.title, aa_search_index.metadata) AGAINST (?)
AND content_type = ?
AND content_id != ?
';
$params = array($query, $query, 'thread', $excludedThreadId);
if (!Search_query::get('options')->showResultsGlobal)
{
$params[] = $forum['node_id'];
$sql .= 'AND aa_thread.node_id = ?
';
}
$sql .= 'LIMIT 5';
$results = $this->fetchAllKeyed($sql, 'content_id', $params);
$threadIds = array_keys($results);
select * from your_table
where column_with_text = 'android'
and datetime_column >= curdate() - interval 1 month
I would like a certain value(55) when there is no result in a MySQL SELECT Statement.
I tried this but it doesn't work.
<?php
include_once("JSON.php");
$json = new Services_JSON();
$con = mysql_connect("XXXXX.com", "XXXXX", "XXXXXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("XXXXX", $con);
$arr = array();
// The following statement runs but when i tried with IFNULL .. doesn't work...
//$rs = mysql_query("SELECT * FROM partie WHERE statut = 'en cours' ORDER BY date ASC LIMIT 1");
$rs = mysql_query("SELECT IFNULL((SELECT * FROM partie WHERE statut = 'en cours' ORDER BY date ASC LIMIT 1),55)");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
You are getting an error, try echo mysql_error();
What you probably want is something like this:
$rs = mysql_query("SELECT * FROM partie WHERE statut = 'en cours' ORDER BY date ASC LIMIT 1");
if( mysql_num_rows($rs) == 0 ) {
echo 55;
}
i need to have a function that will echo the post that has been posted today.
from 00.00 to 23.59 . ihave created a function but it is not giving the result i need
here is my function:
function todayNews() {
$id = mysql_real_escape_string ($id);
$sql = 'SELECT * FROM wp_posts WHERE post_status = "publish" AND post_date = CURRENT_DATE ORDER BY post_date DESC LIMIT 15';
$res = mysql_query($sql) or die (mysql_error());
if (mysql_num_rows($res) !=0):
while ($row = mysql_fetch_assoc($res)) {
$title = ($row['post_title']);
$old_date = $row['post_date']; // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('H:i', $old_date_timestamp);
$mycontent = ($row['post_content']);
$mycontent = strip_tags($mycontent);
$mycontent = substr($mycontent,0,350);
$mycontent = preg_replace("/\[caption.*\[\/caption\]/", '', $mycontent);
$first_img = '';
$my1content = $row['post_content'];
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/img/default.png";
}
echo '
<ul class="li-sub">
<li>
'.$title.'
</li>
</ul>
';
}
else:
echo 'There are no posts for today !';
endif;
} // end
Thank you !
EDIT:
Post_date have this format : Y-m-d H:i
You are comparing POST_DATE which is a datetime with CURRENT_DATE which is a Date , it won't give any result.
you can try the following SQL
$sql = 'SELECT * FROM wp_posts WHERE post_status = "publish" AND DATE(post_date) = CURRENT_DATE ORDER BY post_date DESC LIMIT 15';
This will convert the post_date to only date before comparing with current date.