get each post number of views count pdo - pdo

i am new in pdo. want to echo number of times post viewed. each time a user view it will add +1 autometically. i created a table views that type int length 16. my code is below
<?php
try {
$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate, views FROM blog_posts ORDER BY postID ASC');
while($row = $stmt->fetch()){
echo '<div>';
echo '<h1>'.$row['postTitle'].'</h1>';
echo '<p>Posted on '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
echo '<p>'.$row['postDesc'].'</p>';
echo $row['views']; echo "times";
echo '<p>Read More</p>';
$stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ;
echo '</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

You just prepare update statement not execute so execute the prepare statement .
$stmt = $db->prepare('UPDATE blog_posts SET views = views+1 WHERE postID = :postID') ;
$stmt->execute(array(':postID'=>$row['postID']));

Related

PDO row doesn't exist?

Below is my code:
<?php
$url = $_GET['url'];
$wordlist = array("Www.", "Http://", "Http://www.");
foreach ($wordlist as &$word) {
$word = '/\b' . preg_quote($word, '/') . '\b/';
}
$url = preg_replace($wordlist, '', $url);
?>
<?php
$oDB = new PDO('mysql:dbname=mcnsaoia_onsafe;host=localhost;charset=utf8', 'mcnsaoia_xx', 'PASSWORD');
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt=ja");
$hStmt->execute(array('hjemmside' => $url));
if( $row = $hStmt->fetch() ){
echo "EXIST";
}else{
echo "NOT EXIST";
}
?>
My problem is that it says NOT EXIST, because I know that there is a row which should be found with the following query:
SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt=ja
So why does it say not exist? I have absolutely no idea :(
Instead of
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside
AND godkendt=ja");
try
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside
AND godkendt='ja'");
The left is most likely a column and the right side is a string? I don't speak your language, but this is the first thing coming to my mind.
You should surround with quotes your not integer variable in your query
AND godkendt='ja'
Or maybe let pdo deal with it
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt = :ja");
$hStmt->execute(array(':hjemmside' => $url, ':ja' => 'ja'));
//^ i added : for placeholder here, you missed it
I would also rather check if rows are returned
if($hStmt->$eowVount() > 0){
$row = $hStmt->fetch()
echo "EXIST";
}else{
echo "NOT EXIST";
}

SQL Results Pagination

I'm having a bit of trouble paginating my sql query results using PHP. I have tried a few different solutions but have had no success so far. Would someone be able to help me please? Below is my current code I use to display all results however I'd like them splitting into pages of 50.
<?
//query
$data = mysql_query("SELECT * FROM names");
//counts_result
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "ERROR";
}
else {
echo "<div class='search_result_output'>
SEARCH SUCCESS
</div><hr>";
}
?>
<?
//display results
while($result = mysql_fetch_array( $data ))
{
echo "<div class='panel panel-default'>";
echo "<div class='panel-heading'>";
echo "<h4 class='panel-title'>";
echo $result['name'];
echo "</h4>";
echo "</div>";
echo "<div class='game_actions' style='float:right; margin-top:-40px; margin-right:10px;'>";
echo "<a data-toggle='collapse' class='btn btn-custom' data-parent='#accordion' href='#".$result['id']."' title='More Info on ".$result['name']."'><i class='fa fa-plus'></i></a>";
echo "</div>";
echo "<div id='".$result['id']."' class='panel-collapse collapse'>";
echo "<div class='panel-body'>";
echo $result['name'];;
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>
You have this:
$data = mysql_query("SELECT * FROM names");
instead use this:
$page = (isset($_GET["page"])) ? $_GET["page"] : "0";
$data = mysql_query("SELECT * FROM names limit ".$page.", 50");
Display your links accordingly; add the page value to them. Your paging depends on the get parameter named "page".
EDIT:
If you want to display a link pointing to the 5th page, for instance, then:
echo '5';
4 * 50 = page index * page size. Page index is 4, as on the first page you have no offset. It is difficult to add any more help, since I do not know what kind of paging you need. However, this edit should help you to start solving your problem.
try something like this query and then just just change parameter i.e (3 and 8)
Select * from
(select Id,Name ,ROW_NUMBER() over (order by Id) as RowNum from Person.dbo.pers
) tbl
Where tbl.RowNum between 3 and 8
it gets 3 to 8 rows

Webform : how to count and live display results

Update (that one works)
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load($nid);
if ($node->type == 'webform') {
$count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d', $nid));
$atelier_1 = "sources" ;
$sql = "SELECT count(*) FROM {webform_submitted_data} WHERE data LIKE \"".$atelier_1."\" ;";
$count_atel_1 = db_result(db_query($sql));
}
}
echo $sql;
echo $count_atel_1;
?>
This webform has been submitted <?php print $count ?> times.
We'd like to use a webform so that our students should register on some workshops.
The webform works great. Now we'd like to display live the number of students that are already register in on of the workshops so that the other should know if there remains some possibilities of registration (each workshop can only accept 20 students at the same time)
I'm trying with that that doesn't work ($atelier_1 = "sources" ;) is the name of one workshop :
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load($nid);
if ($node->type == 'webform') {
$count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d', $nid));
$atelier_1 = "sources" ;
$count_atel_1 = db_result(db_query('SELECT count(*) FROM {webform_submitted_data} WHERE data LIKE %d', $atelier_1');
}
}
echo $count_atel_1;
?>
Any help or suggestion welcome
The only way you can show that live, is by creating an ajax in drupal, or with drupal behaviours, or with javascript.

Drupal 7 Multiple Select Joins Syntax

Having real trouble getting my head around multiple joins. Can anyone tell me why this is wrong please?
*EDIT*
$query = db_select('node', 'n');
$query->join('file_usage', 'fu', 'fu.id = n.nid');
$query->join('field_data_field_featured_image', 'ffimage', 'ffimage.entity_id = n.nid');
$query->join('file_managed', 'fm', 'fm.fid = n.nid');
$query
->fields('fu', array('id'))
->fields('fm', array('filename'))
->fields('ffimage', array('field_featured_image_title'))
->fields('n', array('nid', 'title'))
->condition('n.type', 'work')
->orderby('n.nid', 'DESC')
->range(0, 20);
$result = $query->execute();
foreach ($result as $record) {
echo '<li>';
echo '<div class="home_text"><h1>'.$record->title.'</h1></div>';
echo '<a href="...some_node/';
echo $record->id;
echo '">';
echo '<img src="...some_files/';
echo $record->filename;
echo '" width="960px" height="615px" />';
echo '</a>';
echo '</li>';
}

mysql select count resource id #10

When I run the following code the echo returns resource id #10 instead of number, in my case 6. Would appreciate any help!
$result = mysql_query("SELECT COUNT(`category_id`) FROM `products_has_product_category`
WHERE `category_id`=1");
echo $result;
use function mysql_fetch_object or mysql_fetch_row to convert query result to data
$result = mysql_query("SELECT COUNT(`category_id`) as count FROM `products_has_product_category`
WHERE `category_id`=1");
$row = mysql_fetch_object($result);
$count = $row->count;
echo $count;
see mysql_fetch_object