get rid of error before results show up with php - php

the below script throws an error before the results the script executed. Though i get the results i want but i want to get rid of the error that throws up.
When the results equals to 1 the error doesn't show up, but when its more than 1 the error comes.
Notice: Trying to get property of non-object in
C:\xampp\htdocs\server_scripts\complains_list.php on line 1
if(count($customers->User_Name)=='1') {
echo json_encode(array($data));
}else
{
echo json_encode($data);
}

You just need to check that variable is set or not, check manual
if(isset($customers->User_Name) && count($customers->User_Name)=='1')
{
echo json_encode(array($data));
}
else
{
echo json_encode($data);
}

I guess that at some point your $customer isn't instantiated. Thus you have to test for its existence :
if($customer != null && count($customers->User_Name)=='1') {
echo json_encode(array($data));
} else {
echo json_encode($data);
}

Related

Php Json Get Keys

I echoed this and fetched in ajax .
$result= $this->mpesa->STKPushQuery($checkoutRequestID, 174379, "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919");
The results am getting in json is:
{"requestId":"","errorCode":"400.002.02","errorMessage":"Bad Request - Invalid CheckoutRequestID"}
Now in my php code I need to get the Keys of errorCode that sometimes is a successCode so when I try this:
if ($result->errorCode=="400.002.02"){
$Data = '{"status":"Submit payment before Checking for it"}';
echo $Data;
Its fine because the errorCode is found in Json. When there is a success message i.e:
if ($result->successCode=="0"){
$Data = '{"status":"Payment Successful"}';
echo $Data;
}
I get an error with the first statement. Because errorCode is not found in Json
So what I actually need is to get the key of json(which will be either errorCode or successCode)
i.e
$mystatusCode== Get the either the errorCode or SuccessCode (key in Json array[1])
if ($results->mystatusCode=="400.002.02"){
$Data = '{"status":"Submit payment before Checking for it"}';
echo $Data;
}else if ($results->mystatusCode=="0"){
$Data = '{"status":"Payment has been processed successfully"}';
echo $Data;
}
Using isset will accomplish your goal:
$mystatusCode = ( isset($result->errorCode) ? $result->errorCode :
( isset($result->successCode) ? $result->successCode :
NULL
)
);
Or you can just use it in the if statements directly without making a new single var:
if (isset($result->errorCode) && $result->errorCode == "400.002.02") {
...
}
elseif (isset($result->successCode) && $result->successCode == "0") {
...
}

CODEIGNITER - Message: Trying to get property of non-object

I got the following message:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/usermodel.php
Line Number: 4146
and here is my function where the line 4146 is:
else if($resultRes->average<'3' && $resultRes->average>='2')
This is the full function code. I have the same message for line 4137, 4140, 4143, 4146 and 4149
function get_Ranking($resid=''){
$sql="select ((Qrating+Srating+Drating+Crating)/4) as average from testimonial where RestId=".$resid;
$query=$this->db->query($sql);
$resultRes=$query->row();
if($resultRes->average == '5'){
return '050';
}
else if($resultRes->average<'5' && $resultRes->average>='4'){
return '040';
}
else if($resultRes->average<'4' && $resultRes->average>='3'){
return '030';
}
else if($resultRes->average<'3' && $resultRes->average>='2'){
return '020';
}
else if($resultRes->average<'2' && $resultRes->average>='1'){
return '010';
}
else{
return '000';
}
}
How can i fix it?
It's because some of your queries are empty.
if($query->num_rows() != 0)
first
Try with
if($resultRes[0]['average'] == '5'){
return '050';
}
else if($resultRes[0]['average'] < '5' && $resultRes[0]['average'] >= '4'){
return '040';
}
Since it will return an single row only,You can either use $resultRes['average'] directly.
Try to print the $resultRes before getting average from it.
$resultRes=$query->row();
printr($resultRes);
also print the last query and check whether your parameters are correctly populated in the query using below query.
echo $this->db->last_query();
It can be the case your parameter is empty and no record retrieved from table.

Warning: Division by zero in ... on line 133

Basically, i've made a script that login to a website, get elements from the website, and divide one element with another. The problem is, i have already checked plenty of times, and the divisor is NOT zero. here is the snippet:
if($num==0)
{
echo "<td>".$estate_income."/".$num."</td>";
}
else
{
echo "<td>".$estate_income/$num."</td>";
}
this would output something like 50/2000, which means the if statement is true, which means that $num is somehow equal to 0. if i try to divide the two variables with each other, it would output php warning diivision by zero:
echo "<td>".$estate_income/$num."</td>";
What i ask now is, for a solution, maybe some error detection methods that could tell me what i am doing wrong. it's probably something very obvious that i have overlooked.
Thanks in advance!
Ahmad Albayati
Edit:
try
{
echo "<td>".$estate_income/$num."</td>";
}
catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Outputs Warning: Division by zero in . . . on line 133
The variables i am trying to divide is within a foreach loop.
The $num variable is defined in a function that gets included:
function num_format($n)
{
$n=str_replace(",","",str_replace(" ","",$n));
if(strpos($n,".")===FALSE)
{
if(strpos($n,"K")!==FALSE)
{
$n=str_replace("$","",str_replace("K","000",$n));
}
elseif(strpos($n,"mil")!==FALSE)
{
$n=str_replace("$","",str_replace("mil","000000",$n));
}
elseif(strpos($n,"bil")!==FALSE)
{
$n=str_replace("$","",str_replace("bil","000000000",$n));
}
elseif(strpos($n,"tril")!==FALSE)
{
$n=str_replace("$","",str_replace("tril","000000000000",$n));
}
else
{
$n=str_replace("$","",$n);
}
}
else
{
$n=str_replace(".","",$n);
if(strpos($n,"K")!==FALSE)
{
$n=str_replace("$","",str_replace("K","00",$n));
}
elseif(strpos($n,"mil")!==FALSE)
{
$n=str_replace("$","",str_replace("mil","00000",$n));
}
elseif(strpos($n,"bil")!==FALSE)
{
$n=str_replace("$","",str_replace("bil","00000000",$n));
}
elseif(strpos($n,"tril")!==FALSE)
{
$n=str_replace("$","",str_replace("tril","00000000000",$n));
}
else
{
$n=str_replace("$","",$n);
}
}
global $num;
$num=$n;
}
Found the problem. As I have already mentioned, I got the values from a website. When I use var_dumb on the string, it outputs string(149) "202000". This actually had confused me, because the string/number was only 6 characters. so I decided to look at the source code, and I found this:
<spanstyle="white-space:nowrap;"><imgsrc="http: staticstorm8com="" vl="" images="" bloodpng?v="330"width="9"height="12"style="padding-right:2px"">202000<br></imgsrc="http:></spanstyle="white-space:nowrap;">
I got some other HTML elements in my variable when I was taking the values from the website.

How to check if php mysqli_fetch_array is empty before while loop

I wanted to make sure there are results before running the while loop but all the methods I am trying seem to remove the first result.
$nearbyResult = mysqli_query($con,$sqlNearby);
if(mysqli_fetch_array($nearbyResult) == 0) {
echo '<p>No results found, Add your property here.</p>';
} else {
while($rowNearby = mysqli_fetch_array($nearbyResult)) {
}
}
This line will take the first row of your result set and chuck it in the bin:
if(mysqli_fetch_array($nearbyResult) == 0) {
Change to:
if( ! mysqli_num_rows($nearbyResult) ) {
And check your freakin function returns:
if( ! $nearbyResult = mysqli_query($con,$sqlNearby) ) {
echo "Mysql error: " . mysqli_error($con);
}
You can use the mysql_row_count method to count how many rows are returned in your query http://php.net/manual/en/mysqli-result.num-rows.php
Try assigning the results to a variable as part of your if statement. If mysqli_fetch_array() has no result set to work with it will return false.
if($rowNearby = mysqli_fetch_array($nearbyResult)) {
//There was a result, work with it here
doStuffWith($rowNearby);
} else {
//No records in your result set, handle as desired
}

whats wrong with this format?

something is wrong with my code formatting i believe
i am still unsure of what is happening that gives this error,
i am getting the error Parse error: syntax error, unexpected T_VARIABLE, expecting '('
here is my code
<?php
$runamazonapi = false;
if $runamazonapi = true
{
"run this code"
else
}
//do nothing
{
?>
i am getting the following error on line 3 or at this part
if $runamazonapi = true
thanks for your help in advance!!
<?php
$runamazonapi = false;
if ($runamazonapi == true)
{
"run this code"
}
else
//do nothing
{
}
?>
There are a number of syntactical errors with your code, but the error means that the php parser expected to find an ( but instead found a variable. You need () around the if statement condition and you need a closing } on the first if condition. Also, you need to use the proper {} to open and close the else clause:
<?php
$runamazonapi = false;
if ($runamazonapi = true)
{
"run this code"
}
else
{
//do nothing
}
?>
Also, what you have won't work. You're assigning $runamazonapi to true, not checking if it is true. You need to use == not =:
<?php
$runamazonapi = false;
if ($runamazonapi == true)
{
"run this code"
}
else
{
//do nothing
}
?>
try
if($runamazonapi){
//run code
}else{
//do something
}

Categories