3

I don't understand this at all.

I have a class which is roughly the following:

<?php
class pageData
{
    private $Bookmark;
    private $Program;
    private $Agency;

    //With appropriate setters/getters
}
?>

Then I try to create a new object, pass it around a bit, and eventually end up with:

mysql_query("INSERT INTO Records (Bookmark, Program, Agency)
VALUES ('$data->getBookmark()', '$data->getProgram()', '$data->getAgency()')");

I end up with Notice: Undefined property: pageData::$getBookmark in...

Notice: Undefined property: pageData::$getProgram in...

Notice: Undefined property: pageData::$getAgency in...

Using PhpMyAdmin it looks like Bookmark becomes 0 and Program becomes () and Agency is empty.

If I type

print($data->getBookmark());

it prints out the bookmark. if I type

echo $data->getBookmark();

it prints out. Why doesn't it work when I try to insert it into the database, too?

3
  • Advice: Also consider using parameters for your db queries. Commented Jan 29, 2013 at 20:43
  • I need to clean my query up, I was getting frustrated and ended up ripping 90% of it out trying to figure out why it wasn't working. One of those things where you stare at it for too long until it all blurs together and your brain is more than useless ;) Commented Jan 29, 2013 at 20:49
  • 1
    While at it, your should update your code, because the mysql_* functions are deprecated in PHP, are no longer maintained, and may be a security issue. Commented Jan 29, 2013 at 20:56

2 Answers 2

1

When using anything other than a normal variable in a string you should add curly brackets around the values:

mysql_query("INSERT INTO Records (Bookmark, Program, Agency)
             VALUES ('{$data->getBookmark()}', '{$data->getProgram()}', '{$data->getAgency()}')");
Sign up to request clarification or add additional context in comments.

1 Comment

@Drew, Welcome to StackOverflow! You always have the ability to mark your question as answered as described in the FAQ section How to Ask, if the answer solved your problem or was helpful. You can undo this anytime if another more helpful) answer is given. Accepting answers and upvoting helpful answers or good questions is the StackOverflow-way of saying "Thank you". (Of course that doesn't mean that comments like yours are discouraged or not allowed!)
1

It is getting interpreted as a data-member

The variable $data->getBookmark followed by ()

Do

mysql_query("INSERT INTO Records (Bookmark, Program, Agency)
VALUES ('".$data->getBookmark()."',...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.