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.
This question already has an answer here:
Trying to get property of non-object in PDO
(1 answer)
Closed 6 years ago.
I am selecting one column from Member table. Now I want to get the value from this column. But when I try to get the value I am getting an exception as :
<br />
<b>Notice</b>: Trying to get property of non-object in
<b>C:\Program Files (x86)\Ampps\www\MLMapi\PurchaseProduct.php</b> on line
<b>54</b>
<br />
Code:
$stmt = $dbConnection->prepare("select Member.sales_hold_fund from Member where member_id=?");
$stmt->execute(array($member_id));
$member = $stmt->fetch(PDO::FETCH_ASSOC);
$fund = $member-> sales_hold_fund;// line54
$sales_hold_fund = $fund + $amount;
I want to get the sales_hold_fund value from database and add the amount value to the sales_hold_fund.
member value :
"Member": {
"sales_hold_fund": "1000"
}
Can anyone help please? Thank you..
try
$fund = $member['sales_hold_fund'];
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']
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
check when PDO Fetch select statement returns null
(2 answers)
Closed 9 years ago.
I am a beginner with php and PDO and need help solving this error that I keep getting on this line of code
$category_name [$category["category_id"]] = $category["category"]; I continue to get, Notice: Undefined index: category. Also Undefined index: category_id. I dont have a clue as to why, could someone provide me with the correct way to do this. Here is my code. This one slightly different because it used in a while statement.
// get the item category names
$category_name = Array();
$query = 'SELECT * FROM category_2';
$categories_list = $db->prepare($query);
$categories_list->execute();
while ($category = $categories_list->fetchAll())
{
$category_name [$category["category_id"]] = $category["category"];
}
try this:
We will have single array in each time in loop in $category. You can access any database attributes in this array like this: $category['attribute_name']
while ($category = $categories_list->fetch(PDO::FETCH_ASSOC))
{
$category_name [$category["category_id"]] = $category["category"];
}
print_r($category_name);
You're probably looking for fetch(PDO:: FETCH_ASSOC) instead of fetchAll()
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
converting simple query to cake query?
Actually I have 1 query but I am unable to convert it into CakePHP query format.
$result = "select
* from esl_userresults, esl_lyrics
where
esl_userresults.esl_songID = esl_lyrics.id
and esl_lyrics.song_name like '%".$esl_keyword."%'" ;
When I convert this query into CakePHP it gives an error like:
esl_userresults.esl_songID unknown column.
You can execute any query using:
<?php
function some_controller_name()
{
$query_to_execute = 'SELECT * .....';
$results = $this->ModelName->query($query_to_execute);
$this->set('results', $results);
}
?>
Hope this helps