This question already has answers here:
Query doesn't work inside a function
(5 answers)
Closed 8 years ago.
I have this code inside a function:
function verifica($soph,$ano)
{
$sql_ver = "SELECT * FROM ativEns WHERE Sophia_ID = ".$soph." and AnoLetivo =".$ano."";
$lista_ver = sqlsrv_query($ligarBD,$sql_ver);
$result_ver = sqlsrv_fetch_array($lista_ver);
if (!$result_ver)
{
echo "Nothing.";
}
else
{
echo $result_ver["desc"];
}
}
To call the function:
$temp_ver = substr($anoatual,0,4) . substr($anoatual,5,4);
verifica($sophia,$temp_ver);
It works perfectly if not inside the function, but when I put it in the function it just echoes de "Nothing"
$sophia is undefined inside your function, so
$sql_ver = "SELECT * FROM ativEns WHERE Sophia_ID = ".$sophia." and AnoLetivo =".$temp_ver."";
is producing a query that looks like
SELECT ... WHERE Sophia_ID = and
^-----due to undefined variable.
Perhaps it should be just $soph instead, to match the arguments in the function definition?
Related
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 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.
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.
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:
How do you debug PHP scripts? [closed]
(30 answers)
Closed 9 years ago.
i know this must be only a small bug, but i cant find it.
My function:
function del_mysql($table,$id)
{
$id = $_GET['id'];
$exec = mysqli_query($con, "delete from $table where id = '$id'");
return $exec;
}
in Code:
if ($_GET['action'] == 'delete')
{
del_mysql("awsome","$id");
}
if make in function:
$id = $_GET['id'];
echo $table;
echo $id;
i get right table and id.
Somebody see the bug?
I removed already the $exec and return part and leave only mysqli_query command. but dont want to work.
The problem is that in your del_mysql function, you are referencing the connection object $con, which does not exist in the scope of the function. Either pass it into the function as a parameter like this:
function del_mysql($table, $id, $con) {
or access it as a global variable like this:
function del_mysql($table, $id) {
global $con;
I hope that helps.
Regards,
Ralfe