简体   繁体   中英

updating page views in mysql

I'm creating my site located here .

on my singles page ( example ) where it displays only one item, I want it to update the views field in my database called "mobi" with a +1 hit which is currently set as int(6).

I want it to hit for what ever the promo_title is being viewed from the database.

the page connects to the database via the include_once php code on all of my pages.

the code I have been using is this:

$query = $pdo->prepare ("UPDATE mobi SET views = views +1 WHERE promo_title= ? ");
$query->bindValue(1, $promo_title);
$query->execute();

But it does not seem to work.

please can someone tell me where I'm going wrong?

is there an alternative way of doing this?

if you require any more info then please ask and I will update this.

thank you.

What is the value of $query->rowCount(); if you invoke it immediately after the execute() ?

I bet it's zero. I bet your WHERE clause is not matching any rows.

It's possible that the first time you attempt to update the table for a given promo_title , no matching row has yet been inserted. If that's true you have to insert the row.

It's possible that your promo_title column is a CHAR(n) column rather than a VARCHAR(n) column. Try this SQL instead of what you have to test this hypothesis:

UPDATE mobi SET promo_views = promo_views +1 WHERE TRIM(promo_title) = TRIM(?)

Notice that the performance of this will be terrible, as the TRIM function on promo_title definitively defeats the use of any index. But if it works, you'll have a clue about what's wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM