PDO Prepared Statement with parameters array [duplicate] - php

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']

Related

Error handling in MySQL Insert/Update with PHP [duplicate]

This question already has answers here:
Getting raw SQL query string from PDO prepared statements
(16 answers)
Why does this PDO statement silently fail?
(2 answers)
Closed last month.
I'm trying to come up with a way to control what happends if some UPDATE/INSERTS into a BD fail.
private $db;
public function __construct() {
$dsn = 'mysql:dbname=BDNAME;host=localhost';
$this->db = new PDO($dsn, 'USEER', 'PASSWORD', array('charset' => 'utf8'));
}
public function createnewthingtodo($what,$where,$when,$how,$done){
$sql= "INSERT INTO TODOLIST (what,where,when,how,done) VALUES (:what, :where, :when, :how, :done)";
$st = $this->db->prepare ($sql);
$st->bindValue(':what',$what);
$st->bindValue(':where',$where);
$st->bindValue(':when',$when);
$st->bindValue(':how',$how);
$st->bindValue(':done',$done);
if($st->execute()){
return true;
}else{
return false;
}
}
This example is returning always false, if i delete the last "if" and put the execution part just below the last binvalue, it works just fine, but then i end up with no error handling, any ideas on how can i achieve this? Thanks for your time.

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 Notice Trying to get property of non-object [duplicate]

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;
}

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.

binding parameters is causing a warning [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Cannot use call_user_func_array on mysqli_stmt object
I have a piece of mysqli code which is outputting a warning in this line of code:
if (!$stmt = $mysqli->prepare($questionquery)) {
die("Error preparing statement: $mysqli->error");
}
The warning is:
Warning: Wrong parameter count for mysqli_stmt::bind_param() in ... line 80
Below is main code:
// Make the referenced array
$referencedArray = make_values_referenced(array_merge(
array(str_repeat("ss", $numTerms)), // types
$termArray, // where
$termArray // order by
));
// ...or die() is evil in production but I shall assume we are debuggin so I won't complain
if (!$stmt = $mysqli->prepare($questionquery)) {
die("Error preparing statement: $mysqli->error");
}
// Bind parameters
if (!$stmt->bind_param($referencedArray)) {
die("Error binding parameters: $stmt->error");
}
You can call the method directly:
$stmt->bind_param($referencedArray);
EDIT: Actually I'm mistaken. You need call_user_func_array for variable number of parameters. See this answer for a solution: https://stackoverflow.com/a/5108167/163024

Categories