Link to home
Start Free TrialLog in
Avatar of prashanth ag
prashanth ag

asked on

How to Avoid Undefined Index / Offset Errors In PHP

hi,
Plz find the code

public function getLeaveBalance($emp_code, $leave_type)
{
$query = mysqli_query($this->_dbconnect, "SELECT balance FROM leave_balances WHERE code = '$leave_type' AND emp_code = '$emp_code'");
$result = array();
$i = 0;
while($res = $query->fetch_assoc())
   {
  $result[$i] = $res;
  $i++;
}
[b]  return $result[0]['balance'];                   [/b]
	}

Open in new window

error occurs  line (  return $result[0]['balance'];   ) and I don't want  ignore by using
error_reporting = E_ALL & ~E_NOTICE
ASKER CERTIFIED SOLUTION
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prashanth ag
prashanth ag

ASKER

Hi mukesh yadav
thanks for your feed back ,It solved error
but is shows  null vales (balance column)  in front end back end balance column had  values

plz Find my DB
emp_code    code   balance
1003              CL         1.0
1003              EL          35.0
1003              SL         4.0
Since that query returns one field of a unique row, I'd rewrite your code this way:
public function getLeaveBalance($emp_code, $leave_type)
{
$query = mysqli_query($this->_dbconnect, "SELECT balance FROM leave_balances WHERE code = '$leave_type' AND emp_code = '$emp_code'");
[b]  return $query->fetch_object()->balance;[/b]
}

Open in new window

Hi all,
some values are not in the column (balance)
thanks to all
I  try this code return $query->fetch_object()->balance;
but it shows Notice: Trying to get property of non-object in
Try this:
public function getLeaveBalance($emp_code, $leave_type)
{
return mysqli_query($this->_dbconnect, "SELECT balance FROM leave_balances WHERE code = '$leave_type' AND emp_code = '$emp_code'")->fetch_object()->balance;
}

Open in new window

Marco Gasi
Thanks for your feedback
Notice: Trying to get property of non-object

When your script gives this message in the given context, this usually means the query failed.  Information about how to run a query and test for success or failure is available in this article.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
my appln throws  one(new) more error

Fatal error: Call to a member function fetch_assoc() on a non-object in
line 4
$query = mysqli_query($this->_dbconnect, $query);
		$result = array();
		$i = 0;
		[b]while($res = $query->fetch_assoc()){[/b]
			$result[$i] = $res;
			$i++;
		}
		return $result;

Open in new window

As I wrote in my last message, that error tells that the query is malformed.
You are in an object, right? So you could write something like this:

public function getLeaveBalance($emp_code, $leave_type)
{
    $mysqli = $this->_dbconnect;
    return $mysqli->query("SELECT balance FROM leave_balances WHERE code = '$leave_type' AND emp_code = '$emp_code'")->fetch_object()->balance;
}