I do not understand what is wrong with me, I believe that the request to the correct database.
I am trying to increase the counter by one views, while post_id and hosts simply rewritten or left unchanged
when the user selects a specific post views counter shall be increased by one, and another counter hosts remain unchanged.
$current_ip = $wpdb->get_row( $wpdb->prepare("select * from ".$wpdb->prefix."views_date where ip_address='$ip'"));
if($current_ip == '1') {
$wpdb->update($wpdb->prefix."views_count",
array(
'post_id' =>$id,
'hosts'=>'hosts',
'views'=>'views'+1 //not increased by one
)
);
}
"post_id INT(11) NOT NULL, hosts INT(11) NOT NULL, views INT(11) NOT NULL"
Try this: 'views'=> views+1
This is likely failing because if($current_ip == '1') will not be equal to 1.
$current_ip is the results of the complete row of data from the database, so you need to get the data from the IP column, something like:
while($row = $current_ip->fetch_assoc()){
$ip_value_from_database = $row["ip_address"];
}
if($ip_value_from_database == '1') {
//update query
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am checking the for the user_id (it is held in a session) - this is working. Then I am running a SELECT query for that user for the database table click_count. I am checking to see if that user has any records within it, ie: $page_count. If not, I want my INSERT statement to run to add that user to the database table along with other data.
The part I do not understand is it seems that my UPDATE query is always running. For example no matter which user I login with my query only updates the only user in the database table. IE: Bob is the only user in the click_count table, if I log in with Pete, Bob's record is being updated.
I have tested the value for $page_count and it equals 0, so my INSERT should be running. I have also tried if ($page_count === 0) {
Does anyone see anything I am missing?
$curPage = $_SERVER['PHP_SELF'];
$clicks = 0;
$setup = 0;
$page_total_count = 0;
var_dump($user_id);
$click_sql = "
SELECT *
FROM click_count
WHERE user_id = ?
AND page_url = ?
";
$click_stmt = $con->prepare($click_sql);
$click_stmt->execute(array($user_id, $curPage));
$click_stmt_rows = $click_stmt->fetchAll(PDO::FETCH_ASSOC);
$page_count = $click_stmt->rowCount();
foreach ($click_stmt_rows as $click_stmt_row) {
$setup_status = $click_stmt_row['setup'];
$page_total_count = $click_stmt_row['page_count'];
}
if ($page_count == 0) {
$click_insert_sql = "
INSERT INTO click_count
(user_id, page_url, page_count, setup)
VALUES(?, ?, ?, ?)
ON DUPLICATE KEY UPDATE page_count=page_count+1;
";
$click_insert_stmt = $con->prepare($click_insert_sql);
$click_insert_stmt->execute(array($user_id, $curPage, 1, $setup));
}
else {
$click_update_sql = "
UPDATE click_count
SET page_count=page_count+1
WHERE user_id = ?
AND page_url = ?
";
$click_update_stmt = $con->prepare($click_update_sql);
$click_update_stmt->execute(array($user_id, $curPage));
}
Table
click_count
CREATE TABLE `click_count` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`page_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`page_count` int(11) NOT NULL,
`setup` int(5) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`),
UNIQUE KEY `page_url` (`page_url`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Since there is only the one user in the table, there is no record "to insert/update", therefore
ON DUPLICATE KEY UPDATE failed you silently.
A regular UPDATE will suffice:
I.e. and as an example:
UPDATE table SET col_x = 0|1 WHERE col_y = ? // (boolean 0-1)
Note:
If ever you wish to increase a column by counting later on, the syntax would be:
UPDATE table SET col_x = col_x + 1 WHERE col_y = ?
In regards to your asking about how you could improve on your code:
#Fred-ii- Thanks. Yes, it is working now how I want, but if there are ways to improve the code I am always willing to try to learn it. I just remembered people in the past saying that I didn't need the update query at all with the duplicate key update. – Paul
You could use named placeholders :name rather than ? since they are easier to keep track of, but this is of course a matter of opinion that I feel is also shared by many and not just myself.
Footnotes/credits:
I would like to also give credit to the following comment:
"If you always fall into update indicates that $page_count is not zero.. Try to echo() it to see maybe.. I would probably first try to add another user into click_count table and then it may become easier to see where it goes wrong.. – johnyTee"
where the OP responded with:
"#Fred-ii- I figured it out. I used johnyTee's advise and tried adding another user to the database manually and it wouldn't let me because of the unique index for the page_url column. I then removed the unique index from it and now it works perfectly. Thanks for the help! – Paul"
from PHP PDO doc http://php.net/manual/en/pdostatement.rowcount.php
PDOStatement::rowCount() returns the number of rows affected by a
DELETE, INSERT, or UPDATE statement.
if you need th number of rows in select you should use somethings like
$sql = "SELECT *
FROM click_count
WHERE user_id = ?
AND page_url = ?
";
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
It may be '0' (a string). You can use intval to convert it to an integer.
$page_count = intval( $click_stmt->rowCount() );
http://php.net/manual/en/function.intval.php
For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.
try like this:
$sql = "SELECT count(*)
FROM click_count
WHERE user_id = ?
AND page_url = ?
";
if ($res = $conn->query($sql)) {
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 0) {
//insert
}else {
//update
}
}
Trying to make a loop for EditableGrid code.
This is how it looks now.
$grid->addColumn('id', 'ID', 'integer');
$grid->addColumn('site', 'Site', 'string');
So if I need to add a new column to the page, I add a new column in MySQL database and also add a new row in this code, like:
$grid->addColumn('newcolumn', 'A brand new column', 'string');
In order to automatically add new columns to the page I want to make a loop, which gets inputs for the first argument (name of the field in the database) taken from the table:
CREATE TABLE price (
id INT(11) NOT NULL AUTO_INCREMENT,
site VARCHAR(50) NOT NULL,
and the other two arguments (label that will be displayed in the header and data type of the column in MySQL) taken from this table:
CREATE TABLE header (
header_name VARCHAR(50) NOT NULL,
header_type VARCHAR(50) NOT NULL,
Ok, think I found the solution. In order to create the loop, we create 2 queries, which are:
$get=$mysqli->query('SELECT header_name, header_type FROM header');
$get1=$mysqli->query('SHOW COLUMNS FROM price');
then we make a loop
while($row = mysqli_fetch_assoc($get) and $row1 = mysqli_fetch_assoc($get1)){
$grid->addColumn(''.$row1['Field'].'', ''.$row['header_name'].'', ''.$row['header_type'].'');}
I, guess, that's it. Also, if you need to exclude some of the columns, use this piece of code:
if($row1 == 'id' || $row1 == 'site')
continue;
I'm trying to create notification system for my community website, am trying to use a while loop to get data, when ever a condition in the if statement is met within the while loop, it should display/print data to the page. For some reason it's only displaying one result, dunno why.
The structure of my database:
CREATE TABLE IF NOT EXISTS `notifications` (
`notification_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`to_id` int(11) NOT NULL,
`notification_identifier` enum('1','2','3','4','5','6') NOT NULL,
`notify_id` int(11) NOT NULL,
`opened` enum('0','1') NOT NULL,
`timestamp` datetime NOT NULL,
PRIMARY KEY (`notification_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
The notification_identifier tell me what type of notification it is(e.g. profile comment, status update, likes) and the notify_id tells me id of each specific table i need to check with.
My code:
<?
$DisplayNotification ="";
$unread = "0";
$mynotify = mysql_query("SELECT * FROM notifications WHERE to_id='$logOptions_id' AND opened='$unread'") or die (mysql_error());
$notify_Count = mysql_num_rows($mynotify);
if($notify_Count>0){
while($row = mysql_fetch_array($mynotify)){
$notification_id = $row["notification_id"];
$memb_id = $row["user_id"];
$identifier = $row["notification_identifier"];
$notify_id =$row["notify_id"];
$timestamp = $row["timestamp"];
$convertedTime = ($myObject -> convert_datetime($timestamp));
$when_notify = ($myObject -> makeAgo($convertedTime));
if($identifier == 1){// condition 1
$DisplayNotification ='user added you as a friend';
}else if ($identifier == 2) {//condition 2
$DisplayNotification ='user commented on your post';
}
}
}else{// End of $notify
$DisplayNotification ='You have no new notifications.';
}
?>
any help appreciated
Where is $DisplayNotification actually displayed? It's certainly not within the body of your loop.
Each time through the loop you assign $DisplayNotification a new value, which of course replaces the old value. By the time you get done, no matter what's happened, the most recent change is the only one left.
Most likely I suspect you meant to do something like
$DisplayNotification .= "User added you as a friend\n";
The .= will continue adding new text to the same variable throughout the loop.
Or perhaps you could use an array, in which case you'd do
$DisplayNotifications[] = "User added you as a friend";
Then you could display all the items at the end however you'd like.
It looks like you run the while statement fully before actually dumping the variable $DisplayNotification. If that is the case, you're just switching values on the variable during the loop. You either need to store the values to be dumped inside an Array or just dump them inside the loop.
I have this code :
$id = $_GET['id'];
mysql_query("UPDATE threads SET views = views + 1 WHERE id = '$id'");
but when I click refresh over and over the counter increases with each refresh.
How can I protect this counter from this with sessions or any other way?
You could set a session variable with (hasBeenCounted) and if that is set, you do not increment.
if(!isset($_SESSION['hasBeenCounted'])
{
$id = $_GET['id'];
mysql_query("UPDATE threads SET views = views + 1 WHERE id = '$id'");
$_SESSION['hasBeenCounted'] = "counted";
}
EDIT:
session_start():
will need to be at the very top of any page that you intend to use sessions on.
One way to do this is to use sessions. However, in the end, that's not really scalable. As a user moves through your site, you could potentially have a massive session file. Additionally, every time a user creates a new session, you will record that view anyway.
If you only want to record a single view for a single user, you need to store that that user viewed the thread. Depending on whether your site only allows logged in users to view threads, or if your site is open to the public, you'll have to store something unique about each user in a separate table, structured something like this (your mileage will vary on field types):
CREATE TABLE `thread_views` (
`id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`thread_id` INT( 10 ) UNSIGNED NOT NULL ,
`user_id` INT( 10 ) UNSIGNED NOT NULL ,
`ts` TIMESTAMP NOT NULL
)
Every time a user views the thread, you'll need to check this table. If there's no record in thread_views, insert one, and the increment the views column in your threads table. If there is a row in thread_views, then you know the user has already viewed the thread.
I think sessions with cookies are your best shot. You would probably want to set the cookie after the first visit, and then if the cookie is already set you don't update your view counter.
The syntax will be similar to this:
$expire=time()+2592000; //This is set to expire in 1 month (in seconds)
setcookie("visitor", "Already Visited", $expire);
This is how I would implement it:
if !(isset($_COOKIE["visitor"]))
{
$expire=time()+2592000; //This is set to expire in 1 month (in seconds)
setcookie("visitor", "Already Visited", $expire);
$id = $_GET['id'];
mysql_query("UPDATE threads SET views = views + 1 WHERE id = '$id'");
}
$id = $_GET['id'];
if(!isset($_SESSION['hasBeenCounted']) || $_SESSION['hasBeenCounted'] != $id)
{
mysql_query("UPDATE threads SET views = views + 1 WHERE id = '$id'");
$_SESSION['hasBeenCounted'] = $id;
}
I've modified the code and it works now.
This function gives me an infinite loop
function getCats($parent,$level){
// retrieve all children of $parent
$result = "";
$query = "SELECT title,parent_id from t_cats where parent_id = '$parent'";
if($rs = C_DB::fetchRecordset($query)){
while($row = C_DB::fetchRow($rs)){
$result .= str_repeat($parent,$level).$row['title']."\n";
getCats($row['parent_id'],$level+1);
}
}
return $result;
}
here is my db table
CREATE TABLE `db`.`t_cats` (
`ID` int(10) unsigned NOT NULL auto_increment,
`datasource_id` int(10) unsigned default '0',
`version` char(10) character set latin1 default 'edit',
`status` char(10) character set latin1 default 'new',
`modified_date` datetime default NULL,
`modified_by` int(10) unsigned default '0',
`title` char(255) character set latin1 default NULL,
`parent_id` int(11) default NULL,
PRIMARY KEY (`ID`),
KEY `idx_datasource_id` (`datasource_id`)
) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=utf8;
I just want to be able to get my list of categories recursive.
But what am i doing wrong?
EDIT:
function getCats($parent,$level){
// retrieve all children of $parent
$result ="";
$query = "SELECT title,parent_id from t_cats where parent_id = '$parent'";
if($rs = C_DB::fetchRecordset($query)){
while($row = C_DB::fetchRow($rs)){
$result.= str_repeat($parent,$level).$row['title']."\n";
getCats($row['id'],$level + 1 );
}
}
return $result;
}
This line looks wrong:
getCats($row['parent_id'],$level+1);
You should be calling it with the current child ID - at the moment you're calling it with the same ID over and over. Something like this (you need to select the id from your table):
getCats($row['id'], $level + 1);
Edit: you need to update your query to select id:
$query = "SELECT id, title, parent_id from t_cats where parent_id = '$parent' AND id != parent_id";
I've also added a bit to stop it getting into a loop if an item is its own parent.
I found this SitePoint article on "Storing Hierarchical Data in a Database" very helpful. It's all PHP examples, and it will improve the performance of what you're trying to do dramatically.
Maybe one of the items in the db has itself as parent?
I don't know C_DB, but I'd bet that the $rs returned by fetchrecordset is a reference, which means that every invocation of getCats is using the same $rs. Exactly what it will do then is unpredictable, depending on how fetchRow is implemented.
If you want to do this (and recursive closures are a pain in SQL, I know), you should open a new connection inside getCats. and be using a separate connection for each access.
correct answer provided by greg ...
2 side notes:
in the case of infinite loops, track recursion depth (you can conveniently use $level here) or overall invocation count (if you are lazy, since this is a oneliner accessing a global counter), and terminate recursion with an exception, when it reaches a maximum value (in general, 10 is already enough to see the problem, but of course that may vary) ... and then get some debug output ... for example $query or something like "calling getCats($parent,$level)" ... would've shown you the problem in no time in this case ... :)
you should minimize the amount of queries ... traversing a tree like that is quite inefficient ... especially, if the database is on another machine ...
greetz
back2dos
Erm shouldnt it be:
$query = "SELECT title,parent_id from t_cats where id = '$parent'";
And not:
$query = "SELECT title,parent_id from t_cats where parent_id = '$parent'";