Welcome Guest, Not a member yet? Register   Sign In
$this->db->affected_rows()
#1

[eluser]Michael;[/eluser]
I have the following code in my model:

Code:
function add_topic($_POST) {
      $topic_add = $this->db->query("INSERT INTO forum_topics (board_id, user_id, topic, dt_create, dt_update) VALUES ('".$this->dot_common->dbsafe($_POST['board_id'])."', '".$this->dot_common->dbsafe($_SESSION['uid'])."', '".$this->dot_common->dbsafe(trim(htmlspecialchars($_POST['topic'])))."', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
      if ($this->db->affected_rows > 0) { $topic_id = $this->db->insert_id(); return $topic_id; } else { return $topic_id = 0; }
    }

$this->db->affected_rows() will *NOT* return a result... pull it out and the function works fine.

I searched the forums but could not find any bug reports... is this something new? Anyone else have a problem?

CI v1.6.1

Thanks.


EDIT: Spelling
#2

[eluser]Derek Allard[/eluser]
Hey Dixen. Let's work through this.

Could you start by simplifying this example down to the very minimum needed to recreate your error? If you take it out of a model and do it directly in a controller do you experience this? If you don't harvest data for insert, but instead use hard-coded values to you experience this? I want to figure out exactly where this is happening in the most simple example we can get.

Before you run affected rows, what happens if you output $this->db->last_query() to the screen. Does it give you what you expect?

Is there a reason you're directly reading POST and SESSION vars instead of using $this->input->post("foo") or counterparts?
#3

[eluser]Michael;[/eluser]
[quote author="Derek Allard" date="1205996391"]Hey Dixen. Let's work through this.

Could you start by simplifying this example down to the very minimum needed to recreate your error? If you take it out of a model and do it directly in a controller do you experience this? If you don't harvest data for insert, but instead use hard-coded values to you experience this? I want to figure out exactly where this is happening in the most simple example we can get.
[/quote]

Before you run affected rows, what happens if you output $this->db->last_query() to the screen. Does it give you what you expect?

Quote:Is there a reason you're directly reading POST and SESSION vars instead of using $this->input->post("foo") or counterparts?

Honestly, because I'm relatively new to CI and I'm not fully familiar with all it's pieces parts yet. Although, I am using native php sessions because of the limitation of 4kb on CI sessions.

Quote:Before you run affected rows, what happens if you output $this->db->last_query() to the screen. Does it give you what you expect?

Yes:

Code:
INSERT INTO forum_topics (board_id, user_id, topic, dt_create, dt_update) VALUES ('1', '1', 'test', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())

Quote:Could you start by simplifying this example down to the very minimum needed to recreate your error? If you take it out of a model and do it directly in a controller do you experience this? If you don't harvest data for insert, but instead use hard-coded values to you experience this?

This code, still produces no result; which is stripped down as it can get. The query *IS* working as I can view the resulting record that is inserted into the database.

Code:
function add_topic($_POST) {
      $topic_add = $this->db->query("INSERT INTO forum_topics (board_id, user_id, topic, dt_create, dt_update) VALUES ('".$this->dot_common->dbsafe($_POST['board_id'])."', '".$this->dot_common->dbsafe($_SESSION['uid'])."', '".$this->dot_common->dbsafe(trim(htmlspecialchars($_POST['topic'])))."', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
      return $this->db->affected_rows;
    }

If I do the following:

Code:
function add_topic($_POST) {
      $topic_add = $this->db->query("INSERT INTO forum_topics (board_id, user_id, topic, dt_create, dt_update) VALUES ('".$this->dot_common->dbsafe($_POST['board_id'])."', '".$this->dot_common->dbsafe($_SESSION['uid'])."', '".$this->dot_common->dbsafe(trim(htmlspecialchars($_POST['topic'])))."', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
      $topic_id = $this->db->insert_id();
      return $topic_id;
    }

I can print topic_id going into the function, both in the controller, and in the model... and coming out of the model correctly. ONLY affected rows is not producing a result.

And lastly, if I put the following:

Code:
$topic_add = $this->db->query("INSERT INTO forum_topics (board_id, user_id, topic, dt_create, dt_update) VALUES ('".$this->dot_common->dbsafe($_POST['board_id'])."', '".$this->dot_common->dbsafe($_SESSION['uid'])."', '".$this->dot_common->dbsafe(trim(htmlspecialchars($_POST['topic'])))."', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
      $num = $this->db->affected_rows;
      print $num;
#4

[eluser]Michael;[/eluser]
ugh... post limit.

Let me finish the rest of the post.
#5

[eluser]Derek Allard[/eluser]
OK, I guess what I was going for by asking for a simpler example, was one that I and other people could use to verify this. Bebugging is easier with a verifiable example, and things that are easy for people to re-implement are easiest to get them to test.

I don't think this is as simple as you can get it. Here's some examples:

1) You're inserting into 5 fields and not fewer. Does it happen with fewer? Can you recreate this with an insert of only 1 field?

2) You're using some functions that are purely unique to your application; "dot_common" isn't part of CI, and neither is the dbsafe() method. Can you remove those? Does this still happen?

3) You're using data harvested from elsewhere in the process (post for example). Can you replace that with a regular string, and does it still happen?

4) You're using a model. If you drop this code into a controller does it still happen?

I really want to help you get to the bottom of this, but just need to eliminate those things first.
#6

[eluser]Michael;[/eluser]
OMG! I'm going to kick myself for the next month on this one.

Derek, I apologize, but I have wasted your time here because I made a stupid mistake... If you look at that function again, there is a typo in the function .. a silly, and complete moronic typo. I should learn not code for 18 hours straight day in and day out.

Code:
function add_topic($_POST) {
      $topic_add = $this->db->query("INSERT INTO forum_topics (board_id, user_id, topic, dt_create, dt_update) VALUES ('".$this->dot_common->dbsafe($_POST['board_id'])."', '".$this->dot_common->dbsafe($_SESSION['uid'])."', '".$this->dot_common->dbsafe(trim(htmlspecialchars($_POST['topic'])))."', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
      if ($this->db->affected_rows() > 0) { $topic_id = $this->db->insert_id(); return $topic_id; } else { return $topic_id = 0; }
    }

That is the correct function... notice the () after $this->db->affected_rows(). Yeah, I left them off originally.

*smacks forehead* I've been starring at this code for 2 hours trying to figure out WTH.

*sighs*

Sorry again Derek... Thank you for your help.
#7

[eluser]Derek Allard[/eluser]
Hey man, no problem. It happens to all of us, and I'm glad for whatever small role I was able to play helping out.
#8

[eluser]Avatar[/eluser]
hahahahhaha, That's funny. This has happened to me before as well. lol Smile Thanks for dot_common I will google it's nature and uses.

EDIT: That sucks it's only one result.
#9

[eluser]Michael;[/eluser]
rofl... too funny. dot_common was just the name that I used for the common library for the portal that I am developing. My company, DotConcepts, is where the "dot" comes from. For a time everything was being tossed into dot_common, but now I have segmented a fair portion of thing out into their own libraries.




Theme © iAndrew 2016 - Forum software by © MyBB