prashanth ag
asked on
How to Avoid Undefined Index / Offset Errors In PHP
hi,
Plz find the code
error_reporting = E_ALL & ~E_NOTICE
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]
}
error occurs line ( return $result[0]['balance']; ) and I don't want ignore by using error_reporting = E_ALL & ~E_NOTICE
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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]
}
ASKER
Hi all,
some values are not in the column (balance)
thanks to all
some values are not in the column (balance)
thanks to all
ASKER
I try this code return $query->fetch_object()->ba lance;
but it shows Notice: Trying to get property of non-object in
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;
}
ASKER
Marco Gasi
Thanks for your feedback
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
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
ASKER
my appln throws one(new) more error
Fatal error: Call to a member function fetch_assoc() on a non-object in
line 4
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;
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_objec t()->balan ce;
}
You are in an object, right? So you could write something like this:
public function getLeaveBalance($emp_code,
{
$mysqli = $this->_dbconnect;
return $mysqli->query("SELECT balance FROM leave_balances WHERE code = '$leave_type' AND emp_code = '$emp_code'")->fetch_objec
}
ASKER
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