PHP Notice Trying to get property of non-object [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I seem to be receiving a number of php notices advising 'Trying to get property of non-object'.
I assume it is the way I have structured the mysql statement but I am a little unsure and I am after assistance here.
The function is as follows:-
public function getPreviousBlock($iHeight=0) {
$stmt = $this->mysqli->prepare("
SELECT height
FROM " . $this->block->getTableName() . "
WHERE height < ?
ORDER BY height DESC
LIMIT 1");
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $iHeight) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_object()->height;
return $this->sqlError();
}
Any help would be much appreciated.

fetch_object() will return NULL if there are no more rows in the result set. Of course NULL is not an object so you will get this error.
So you need to check for example:
$obj = $result->fetch_object();
if ($obj) {
return $obj->height;
} else {
return null;
}

Related

Trying to get property of non-object in on line 20 [duplicate]

This question already has answers here:
How to force PDOStatement->fetchAll to return array of objects?
(3 answers)
Closed 3 years ago.
I'm getting an error and I don't really know where is the issue. Please can anybody show me what is wrong? I would appreciate any assistance, thanks!
Trying to get property of non-object in on line 20
class.php
class PostsData extends dbh {
public function fetchAllPosts() {
$sql = "SELECT * FROM post";
$stmt = $this->connect()->query($sql);
$stmt->execute([]);
$result = $stmt->fetchAll();
return $result;
} }
blog.php
$post_ = new PostsData;
$allposts = $post_->fetchAllPosts();
foreach ($allposts as $post) {
echo $post->post_title; //error
You are not checking if the result returned is null or not. The error would be generated if it is because there will be no property to access altogether.
Consider a print statement in FetchAllPosts function to check if you get any rows returned. That may help narrow down the scope of the error.

PHP recursive function is not returning any value [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I write this recursive function, but it’s not working. I am not able to find the mistake. It’s not returning anything. But if I write an "echo" on the else statement, it’s printing perfectly.
function find_parent($referralid, $leg)
{
$query = "SELECT memberid FROM memberdetails where parentid='" . $referralid . "' and leg='" . $leg . "'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0)
return $referralid;
else
{
while($row = mysql_fetch_array($result))
{
find_parent($row[0], $leg);
}
}
}
When you have recursion, you need to pass the value back as well, otherwise the value isn't propagated back up. So the line which is...
find_parent($row[0],$leg);
needs to be
return find_parent($row[0],$leg);
This is assuming that you will only ever get 1 row as a parent.

Fatal error: Call to a member function execute() on boolean in /home/*****/public_html/lnews.php on line 22 [duplicate]

This question already has answers here:
Call to a member function execute() on boolean in [duplicate]
(5 answers)
Closed 5 years ago.
code:
$results = $mysqli->prepare("SELECT news_title FROM news ORDER BY posted_time DESC");
$results->execute();
$results->bind_result($news_title);
When I execute the code then it will return Fatal error: Call to a member function execute() on boolean in /home/*****/public_html/lnews.php on line 22. So, How can I fix this issue ?please help
Thank You
There is some error from database due to this the prepare method returns FALSE, on success this method returns prepared statement.
You can see the logs to determine the database error. Also, you should change your code in the following way and throw some exception in else.
$results = $mysqli->prepare("SELECT news_title FROM news ORDER BY posted_time DESC");
if($results !== false) {
$results->execute();
$results->bind_result($news_title);
}
In this case if false return (if there is database error) the execute method would not be invoked.

mysqli_result is not working properly? [duplicate]

This question already has answers here:
Fatal error: Call to undefined function mysqli_result()
(2 answers)
Closed 8 years ago.
There is a table 'hit_count' containing only one column 'count' in database in which I am trying to count the hits by user. problem is whenever I am running this code it shows an error message "Fatal error: Call to undefined function mysqli_result()". Please help!!
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'connect.inc.php';
function update_count()
{
global $link;
$query = "SELECT `count` FROM `hit_count`";
if($query_run = mysqli_query($link,$query) || die(mysqli_error($link)))
{
echo 'checking control';
$count = mysqli_result($query_run,0,'count');
echo $count;
}
else
{
echo 'Problem Occured!!';
}
}
update_count();
?>
There isn't a mysqli_result function (not that you can't define it, but what for?). There is a mysqli_result Class, and it has static methods that you can call, of course. But I believe you're doing this wrong.
The correct way would be something like
$count=array();
if($query_run = mysqli_query($link,$query) || die(mysqli_error($link))) {
while ($row = $query_run->fetch_array(MYSQLI_ASSOC)) {
$count[]=$row["count"];
}
}
remember that the outcom of mysqli_query will be an iterable object. Don't expect it to return an aggregate value by default.
PD: if you name your columns with reserved words like count, you're gonna have a bad time.

PDO Prepared Statement with parameters array [duplicate]

This question already has answers here:
Creating default object from empty value in PHP?
(18 answers)
Closed 8 years ago.
I am stuck. I have spent two days looking thru all the references I can find and I can’t figure out why this will not work! I get the error: "Creating default object from empty value." Bellow is my SQL statement and my parameters array.
$sql_insert = "
INSERT INTO vrm_vrd_submission_tbl (vrm_vrd_nmbr_id, vrm_vrd_sub_type_id, vrm_vrd_sub_date, vrm_vrd_min_form_date, vrm_vrd_sub_quantity, county_id, pers_emp_pre_id, election_general_info_id ,vrm_vrd_sub_submitter_name, vrm_vrd_compliance_rules_id)
VALUES(:vrm_vrd_nmbr_id,
:vrm_vrd_sub_type_id,
:vrm_vrd_sub_date,
:vrm_vrd_min_form_date,
:vrm_vrd_sub_quantity,
:county_id,
:pers_emp_pre_id,
:election_general_info_id,
:vrm_vrd_sub_submitter_name,
:vrm_vrd_compliance_rules_id)
";
$sql_parms=array(":vrm_vrd_nmbr_id"=>$vrm_vrd_nmbr_id, ":vrm_vrd_sub_type_id
"=>$data['vrm_vrd_sub_type_id'],
":vrm_vrd_sub_date"=>trim($data['vrm_vrd_sub_date']),
":vrm_vrd_min_form_date"=>trim($data['vrm_vrd_min_form_date']),
":vrm_vrd_sub_quantity"=>trim($data['vrm_vrd_sub_quantity']), ":county_id
"=>$data['county_id'],":pers_emp_pre_id "=>$data['pers_emp_pre_id'],
":election_general_info_id"=>$election_general_info_id,
":vrm_vrd_sub_submitter_name"=>$vrm_vrd_sub_submitter_name,
":vrm_vrd_compliance_rules_id"=> $vrm_vrd_compliance_rules_id);
$ret_val=$db->db_bound_query($sql_insert, $sql_parms);
Method being called in my database class:
public function db_bound_query($qry_str, $parms_array){
$log = new error_log_class;
$db_conn = self::_connect();
if(!$exec_str= $db_conn->prepare($qry_str)){
$log->save_to_log($qry_str,__LINE__,__FILE__,"Failed to perpare.");
}
$val="";
foreach($parms_array as $parm ->$val){
$exec_str->bindParam($parm,$val);
}
$res=$exec_str->execute();
$results= $exec_str->fetchAll(PDO::FETCH_ASSOC);
}
EDIT:
I changed this method to the following as suggensted by #iamsleepy and #MrCode. But I am getting the error I was originally chasing which is "Invalid Parameter number".
public function db_bound_query($qry_str, $parms_array){
$log = new error_log_class;
$db_conn = self::_connect();
if(!$exec_str= $db_conn->prepare($qry_str)){
$log->save_to_log($qry_str,__LINE__,__FILE__,"Failed to perpare.");
}
$res=$exec_str->execute($parms_array );
$results= $exec_str->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
You have a space at the end of this parameter name:
":pers_emp_pre_id "=>$data['pers_emp_pre_id']
^ here
Should be:
":pers_emp_pre_id"=>$data['pers_emp_pre_id']

Categories