473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

a question about mysql_affected_rows()

Ben
Hi all,
I want to store unique data about my page visit. This is done using
cookie value. In my database, the page name and cookie value(ip
address) are both primary keys. This ensures unique entry. The
following code works fine:
$query = "INSERT INTO counter SET page='$currentfile',
uid='$cookie_val',
referer='$ref', count = 1, accesstime='$fileatime' ON
DUPLICATE KEY
UPDATE referer='$ref', count=count+1,
accesstime='$fileatime'";
$result = mysql_query($query, $link) or die("Could not insert");

But I'd also like to know how do I achieve the same effect using
mysql_affected_rows() function. I tried doing as follows but it
doesn't seem to work:
if (mysql_affected_rows($link) < 1) {
$query = "INSERT INTO counter VALUES ('$currentfile', '$cookie_val',
'$ref', 1, '$fileatime')";
$result = mysql_query($query, $link) or die("Could not
insert");
}
else {
mysql_query("UPDATE counter SET count=count+1, referer=$ref,
accesstime=$fileatime WHERE page=$currentfile AND
uid=$cookie_val",
$link);
}
As you can see, if there are no rows, I'd like to "insert", if not,
"update". Problem here is, the code somehow does not go inside else
block. And I echoed the mysql_affected_rows()'s value. It gives me -1.
What am I doing wrong? How do I fix it?

I am using php version 5.0.1 and mysql version 5.0.0-alpha in WinXP.

Thanx!
Ben
Jul 17 '05 #1
1 1948
Hi everybody,

this is a very common newbie problem. The nature of numerous problems
requires inserting a new record into a table if it does not exists and
yet updating it if there is already one. As of MySql 4.1.0 it is
possible to render the following query:

INSERT INTO statistics (counter) VALUES (1) ON DUPLICATE KEY UPDATE
counter=VALUES(counter)+1

This is actually a correct/optimized implementation of a REPLACE query
as it works around the DELETE/INSERT race condition.
Best Regards,

Lucas
Shawn Wilson <sh***@glassgiant.com> wrote in message news:<41***************@glassgiant.com>...
As you can see, if there are no rows, I'd like to "insert", if not,
"update". Problem here is, the code somehow does not go inside else
block. And I echoed the mysql_affected_rows()'s value. It gives me -1.
What am I doing wrong? How do I fix it?


I don't know what the problem is with your code, but you might consider using
REPLACE instead of 2 INSERT/UPDATE queries.

Read the comments here for more information:
http://ca3.php.net/manual/en/functio...ected-rows.php
Shawn

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
3
by: Ben | last post by:
Hi all, I want to store unique data about my page visit. This is done using cookie value. In my database, the page name and cookie value(ip address) are both primary keys. This ensures unique...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
5
by: Tyno Gendo | last post by:
Two questions in a row.. I'm on a roll :) When i perform UPDATEs in my admin system, I'm checking for success by using mysql_affected_rows() and this works fine if data is changed and updated. ...
5
by: MS | last post by:
Hi, I'm doing a table insert and have a question about mysql_affected_rows(). The following code works. It correctly inserts the row and mysql_affected_rows() returns 1 as it should do. ...
3
by: Sandman | last post by:
Hi, So I read the manual where it says to use mysql_affected_rows() for everything except SELECT and SHOW, and use mysql_num_rows() for those two, which actually return a result. However, I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.