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
prashanth agsoftware engineer Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mukesh YadavFull Stack DeveloperCommented:
replace line 4 with this one
$result = array(array("balance"=>0));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prashanth agsoftware engineer Author Commented:
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
0
Marco GasiFreelancerCommented:
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

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

prashanth agsoftware engineer Author Commented:
Hi all,
some values are not in the column (balance)
thanks to all
0
prashanth agsoftware engineer Author Commented:
I  try this code return $query->fetch_object()->balance;
but it shows Notice: Trying to get property of non-object in
0
Marco GasiFreelancerCommented:
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

0
prashanth agsoftware engineer Author Commented:
Marco Gasi
Thanks for your feedback
0
Ray PaseurCommented:
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
prashanth agsoftware engineer Author Commented:
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

0
Marco GasiFreelancerCommented:
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;
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.