Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert data from a class into a database using PHP

Tags:

php

mysql

class

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?

like image 811
Drew Avatar asked Apr 10 '26 05:04

Drew


1 Answers

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()}')");
like image 199
Spontifixus Avatar answered Apr 11 '26 18:04

Spontifixus