Error when trying to recover object from database - php

According to Symfony2 documentation, I can access to instances in database with the next code:
public function showAction($id)
{
$producto = $this->getDoctrine()
->getRepository(’AcmeGuardaBundle:Producto’)
->find($id);
$nombreCategoria = $producto->getCategoria()->getNombre();
// ...
}
However, I try to do:
$imagen = $this->getDoctrine()
->getRepository(’MSDHomeBundle:Imagen’)
->find($_SESSION['id']);
and I get the next error in the browser:
Parse error: syntax error, unexpected ':' in /home/manolo/MiServer/itransformer-2.0/src/MSD/HomeBundle/Controller/HomeController.php on line 219
What am I doing wrong? Imagen is a class stored in database, and persisting data works right.

Related

syntax error, unexpected token "<<", expecting "function" or "const"

I read this article after upgrading my local dev environment to php8.
And implement it in my class, like:
<<Attribute('foo')>>
public function index()
{
$posts = (new Post)->get()->withMany('image');
return $this->app()->view('index', compact('posts'));
}
and it return error.
syntax error, unexpected token "<<", expecting "function" or "const"
What is really a proper of doing that in php8. Am i missing something?
Correct attribute syntax in PHP 8 is:
#[Attribute('foo')]
https://www.php.net/releases/8.0/en.php
public function create(Request $request)//create object of the request
{
$student = new Student;//create object of the model
$student->name = $request->name;
$student->city = $request->city;
$student->marks = $request->marks;
$student->save();
return redirect('index');//if we write this out of the curly braces then this error show
}

PHP how to have declare a class member?

Slightly confused with PHP because it does not declare object variable types.
This is simplified code which does not work. I get why I get an error but not sure how in PHP I can specify that $pb is a PushBot object and so it has methods which can be used.
class Bot
{
public $pb;
//Constructor
function __construct(){
require_once('../PushBots.class.php');
// Application ID
$appID = '';
// Application Secret
$appSecret = '';
// Push The notification with parameters
$this ->pb = new PushBots();
$this ->pb->App($appID, $appSecret);
}
//Method. The $this->pb->Push() does not work
public function sendMessage(){
$this->pb->Push();
}
}
//Client calling the class
$bot = new Bot();
$bot->sendMessage();
The error I get is :
Parse error: syntax error, unexpected '$' for when the line
$this->pb->Push();
is called.
I guess its because it does not know that $pb is a PushBot object at this stage ?
Can I not declare it something like :
Public Pushbot $pb;
Parse error: syntax error, unexpected '$' for when the line
This is a parse error, meaning your code has not even been run yet. Check your code for any sytnax errors (the code you posted does not contain the syntax error).
how in PHP I can specify that $pb is a PushBot object
Although unrelated to the syntax error, if you were using some sort of dependency inversion you could use type-hinting to require a certain object to be passed:
// will cause fatal error if anything other than an object of type Pushbot is passed
function __construct(Pushbot $pb)

Cannot access empty property - Joomla! JDatabaseMysqli

I was receiving the following fatal error, running Joomla 2.5, but only while trying to access the administrative view of a custom component I have created (which accessed the database):
Fatal Error: Cannot access empty property in \libraries\joomla\database\database\mysqli.php on line 498"
The context of line 498 is:
protected function fetchObject($cursor = null, $class = 'stdClass'
{
return mysqli_fetch_object($cursor ? $cursor : $this->cursor, $class);
}
Bizarrely, even after removing the $this->cursor statement like so:
protected function fetchObject($cursor = null, $class = 'stdClass'
{
return mysqli_fetch_object($cursor, $class);
}
I received the same error, despite the fact that that line no longer contains a member access operator.
How could I be receiving this error even though no properties are being accessed in that line?

Issue in the Mapper Code in Zend Frame Work

Notice: Undefined variable: entryMessage in /var/www/Employee/application/models/EmployeeMapper.php on line 34 Fatal error: Call to a member function setEmployeeId() on a non-object in /var/www/Employee/application/models/EmployeeMapper.php on line 34
This is the error i am getting i try to display the entered fields , I checked the database and the fields are getting saved , I am posting the Employee Mapper .Please check the code and tell a solution , thanks in advance
class Application_Model_EmployeeMapper
{
protected $_dbTable;
public function setDbTable($dbTable)
{
if (is_string($dbTable)) {
$dbTable = new $dbTable();
}
if (!$dbTable instanceof Zend_Db_Table_Abstract) {
throw new Exception('Invalid table data gateway provided');
}
$this->_dbTable = $dbTable;
return $this;
}
public function getDbTable()
{
if (null === $this->_dbTable) {
$this->setDbTable('Application_Model_DbTable_Employee');
}
return $this->_dbTable;
}
public function fetchAll()
{
$table = $this->getDbTable();
$resultSet = $table->fetchAll($table->select()->order('EMPLOYEE_ID'));
$entries = array();
foreach ($resultSet as $row) {
$entry = new Application_Model_Employee();
$entryMessage->setEmployeeId($this->$row->EMPLOYEE_ID)
->setFirstName($row->FIRST_NAME)
->setLastName($row->LAST_NAME)
->setEmail($row->EMAIL)
->setPhoneNumber($row->PHONE_NUMBER)
->setHireDate($row->HIRE_DATE)
->setJobId($row->JOB_ID)
->setSalary($row->SALARY);
$entries[] = $entry;
}
return $entries;
}
public function save(Application_Model_Employee $employee)
{
$data = array(
'EMAIL' => $employee->getEmail(),
'FIRST_NAME' => $employee->getFirstName(),
'LAST_NAME' => $employee->getLastName(),
'PHONE_NUMBER' => $employee->getPhoneNumber(),
'HIRE_DATE' => $employee->getHireDate(),
'JOB_ID' => $employee->getJobId(),
'SALARY' => $employee->getSalary(),
);
Error you're getting is very clear, you should always read and try to understand the error message that you get, it gives you solution to most of the common problems. So always make a habit of reading and understanding the error message you get. Here,
Notice: Undefined variable: entryMessage in /var/www/Employee/application/models/EmployeeMapper.php on line 34
Notice says, undefined variable; which means that your variable $entryMessage is not defined. And the second part of the error that you are getting:
Fatal error: Call to a member function setEmployeeId() on a non-object in /var/www/Employee/application/models/EmployeeMapper.php on line 34
is very obvious since you are trying to call setEmployeeId() function from $entryMessage which has not been defined, yet you are assuming it to be an object and using it as an object.
I am assuming that $entryMessage you are using should have been $entry
These are quite simple and are not suitable to be asked in SO actually.
Once again, my suggestion to you are,
Always read what the error message says, it is what you are supposed to be using for debugging any bugs in the code
If you are new to programming you should perhaps use IDE's (netbeans,
eclipse, etc..) which actually shows the usage of variables and
function and syntax error in your code.
Always do your part of research and ask question only if not found or successful, and always show what you attempted. Others are there to help you but not to solve everything for you. :)

php Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION on construct

hey guys was hoping you could help me out..
just to let u know in advance, im a relatively new php coder, doing a practice project, and came across this problem and ive spent like an hour of rechecking and googling but just cant figure out whats causing it
error: Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\wamp\www\forum\classes\ClassUser.php on line 7
the segment of the code causing the problem:
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
Class User{
__construct($u,$p){ //this is line 7
$user=$u;
if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
$password=0;
}
else{
$password=hash_hmac('md5',$p,KEY);
}
}
oh and since im new to php, incase im doing something which i should not be, please to recommend.. thanks in advance.
note:ive removed the php tags since they seemed to be messing with the formatting of this post :/
note2: im also getting another notice
Notice: Use of undefined constant KEY - assumed 'KEY' in C:\wamp\www\forum\classes\general.inc on line 20
but im assuming thats more of a warning than an error... but just adding incase it has something to do with the error
general.inc:
//error definations
define("ERROR_FIELD_EMPTY","Error! All required fields not filled");
define("ERROR_INVALID_SIGNIN","Error! Username/password do not match!");
define("ERROR_GENERAL_INPUT", "Error! Invalid input given");
define("ERROR_SQL_CONNECT","Error! Could not connect to sql database");
//field sizes
define("PASSWORD_LENGTH",12);
define("USERNAME_LENGTH",30);
//sql server details
define("SQL_SERVER_NAME","localhost");
define("SQL_SERVER_USERNAME","root");
define("SQL_SERVER_PASSWORD","");
define("SQL_SERVER_DATABASE","forums");
define(KEY,"key");
function __autoload($className){
require_once($_SERVER["DOCUMENT_ROOT"]."forum/classes/Class$className.php");
}
ClassUser.php
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
Class User{
__construct($u,$p){
$user=$u;
if(strlen($p)>30|| empty($p) || !preg_match('/[^a-zA-Z0-9]/i',$p)){
$password=0;
}
else{
$password=hash_hmac('md5',$p,KEY);
}
}
public function validate(){
if(strlen($user)>30|| empty($user) || preg_match('/[^a-zA-Z0-9]/i',$password==0 )){
throw new Exception(ERROR_GENERAL_INPUT);
}
$user=mysql_real_escape_string($user);
return true;
}
public function insert(){
// this->validate();
$conn= mysqli_connect(SQL_SERVER_NAME,SQL_SERVER_USERNAME,SQL_SERVER_PASSWORD,SQL_SERVER_DATABASE);
if(empty($conn)){
throw new Exception(ERROR_SQL_CONNECT);
}
$query="INSERT into USERS VALUES ($user,$password)";
$conn->query($query);
}
private $user;
private $password;
};
NewUser.php
include $_SERVER["DOCUMENT_ROOT"]."forum/classes/general.inc";
try{
$user = new User($_POST['UserName'],$_POST['Password']);
$user->insert();
}
catch(Exception $Error){
echo $Error->getMessage();
}
Your __construct needs to have the word function in front of it, or better yet public function, in the same way as the other methods in the class (ie validate and insert in your case).
ie you need the following:
public function __construct($u,$p){ //this is line 7
Change the line to:
public function __construct($u, $p) {

Categories